Ask Your Question

Add files with puppet and then execute a command on them

asked 2014-09-11 20:19:31 -0600

Aykyu gravatar image

I'm migrating our old process of doing our linux configurations to be managed by puppet but I'm having issues trying to figure out how to do this. We add some custom scripts to the init.d folder on our systems to manage some processes, and then these need this command executed on them to launch on startup:

update-rc.d $file defaults

So what I'm doing with puppet is that I have all these scripts residing a directory, and I copy them over to init.d. I then want to call 'exec' on each of these files with the former command and use the file name as an argument. This is what I have so far:

#copy init files    
file { '/etc/init.d/':
      ensure => 'directory',
      recurse => 'remote',
      source => ["puppet:///files/init_files/"],
      mode => 755,
      notify => Exec[echo],

exec { "echo":
  command => "update-rc.d $file defaults",
  cwd => "/tmp", #directory to execute from
  path => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:",
  refreshonly => true

This will copy all the files and it calls exec when something is added/updated, but what I can't figure out is how to pass the name of the file as an argument into the exec command. It seems I'm really close but I just can't find anything to help with what I need to do. Is this the right way to try and achieve this?


edit retag flag offensive close merge delete


Typically puppet follows the pattern of Package, File, Service. Where one uses a package resource to install software, and then a file resource to customize any configuration files and then a service resource to start services.

Red Cricket gravatar imageRed Cricket ( 2014-09-12 01:09:28 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-09-12 08:21:42 -0600

llowder gravatar image

You'll want to create a defined resource type, and then in that defined type you will have a file resource and an exec.

This will look something like:

define my_module::my_define {
  file { "place_${title}":
    ensure => 'file',
    path => "/etc/init.d/${title}",
    source => "puppet:///my_module/init_files/$title",
    mode => 755,
    notify => Exec[update_$title],
  exec { "update_${title}":
    command => "update-rc.d ${title} defaults",
    cwd => "/tmp", #directory to execute from
    path => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:",
    refreshonly => true

You could then pair the above defined type with the generate() and parsejson() (or parseyaml(), both from the stdlib module) functions to make adding a new file as simple as dropping it into my_module/files/init_files and doing a puppet run.

The generate() function would call a shell script on the master that would scan the init_files directory and build a list of file names and then pass that back to the master in either YAML or JSON format as an array, then this would be converted into a Puppet array using either parseyaml() or parsejson() and the resulting array would be used to declare the defined type.

$my_init_list = parseyaml(generate('/path/to/'))
my_module::my_define{ $my_init_list: }
edit flag offensive delete link more


If I wish to have the script executed on each run what do I change?

nicccoX gravatar imagenicccoX ( 2015-08-19 04:08:23 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2014-09-11 20:19:31 -0600

Seen: 5,492 times

Last updated: Sep 12 '14