1

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?

2 Answers 2

1

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",
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can also use the built-in function find_file:

if find_file('path-to-your-file') {
  # do something here if your file exists
}

Docs: https://www.puppet.com/docs/puppet/5.5/function.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.