automationpuppetdevops

Puppet conditional only if file exists in a particular directory


I have the following file resource in my puppet manifest:

file{
  'example.dat'
  path   => "/path/to/example.dat",
  owner  => devops,
  mode   => "0644",
  source => "/path/to/example.txt" 
}

I want to run the above snippet only when the .txt file is present in a particular directory. Otherwise, I do not want the snippet to run.

How do I go about doing that in puppet?


Solution

  • In general, you would create a custom fact that returns true when the directory in question satisfies your condition. For example:

    Facter.add('contains_txt') do
      setcode do
        ! Dir.glob("/path/to/dir/*.txt").empty?
      end
    end
    

    Then you would write:

    if $facts['contains_txt'] {
      file { 'example.dat':
        path   => "/path/to/example.dat",
        owner  => devops,
        mode   => "0644",
        source => "/path/to/example.txt",
      }
    }