1

I'm trying to set up a Puppet cron job with the following structure:

file { '/usr/local/sbin/file.py':
  mode    => '0755',
  source  => 'puppet:///modules/file.py',
  require => File['/usr/local/sbin']
}

cron { "cronjob":
  require => "ALL_THE_FILES_ABOVE"
  command => "...command_to_run_script..."
  minute => '*/1'
}

All of the above is in one file run_script.pp. I'm wondering how I can code the require => "ALL_THE_FILES_ABOVE" part.

Thanks!

Kevin Lin
  • 681
  • 5
  • 11

2 Answers2

2

Based on the information provided in your question, I am going to make the assumption that the contents of run_script.pp is many file resources and the listed cron resource. You state that you want the cron resource there to require all of the file resources in that class. Based on this, here is a clean and efficient solution.

There are a few complicated/advanced ways to arrive at a clean and efficient solution, but the easiest to understand is to use a resource default: https://puppet.com/docs/puppet/5.3/lang_defaults.html

With this, we can establish attribute/value pair defaults for all file resources contained in that scope. This would make it easier to use the before metaparameter on the file resources instead: https://puppet.com/docs/puppet/5.3/metaparameter.html#before

This simplifies the solution to a one-liner in your class:
File { before => Cron['cronjob'] }

Note there will be a caveat to this method, which is that if you are declaring, requiring, or containing a class within this manifest, then this default could be expanded to that "area of effect" and cause a circular dependency. In that case, you should use a per-expression resource default attribute: https://puppet.com/docs/puppet/5.3/lang_resources_advanced.html#per-expression-default-attributes

Matthew Schuchard
  • 25,172
  • 3
  • 47
  • 67
0

You can use a multiple require

file{'path/foo':}
file{'path/bar':}
file{'~/foobar':
  require => [ File['path/foo'], File['path/bar'] ]
}

or you can use the ordering arrow

-> (ordering arrow; a hyphen and a greater-than sign) — Applies the resource on the left before the resource on the right.

 file{'path/foo':} ->
   file{'path/bar':} ->
   file{'~/foobar':}

Here is more information about relationships and ordering in Puppet

Stranger B.
  • 9,004
  • 21
  • 71
  • 108