Correct way to add commands to to rc.local above "exit 0" with puppet?

Solution 1:

You can use the PuppetLabs Concat module to dynamically insert parts into the rc.local file, like so:

Define the file as a concat file, and add a header (hash-bang, "managed by Puppet" comment, etc) - note the below may not work, I'm unsure of Puppet's newline handling off the top of my head!

concat { '/etc/rc.local':
  ensure => present,
}

concat::fragment { '00_rc.local_header':
  target  => '/etc/rc.local'
  content => '#!/bin/bash\n# This file is manageed by Puppet, do not modify!',
  order   => '01'
}

Add your custom parts, making sure to specify the correct target and an order value that is higher than your header:

concat::fragment { '05_rc.local_custom':
  target  => '/etc/rc.local',
  content => template('path/to/template.erb'),
  order   => '05'
}

concat::fragment { '15_rc.local_custom_2':
  target  => '/etc/rc.local',
  source  => 'puppet:///modules/mymodule/myfile.txt',
  order   => '06'
}

Add the exit 0 footer:

concat::fragment { '99_rc.local_footer':
  target  => '/etc/rc.local',
  content => 'exit 0\n',
  order   => '99'
}

Et fin!

You can use concat::fragment across multiple subclasses (and also multiple modules, though this really isn't recommended!) as long as you set the correct target value and you have that file declared as a concat resource. The order parameter allows you to position elements within the file.

Check the module's usage readme for more.

Solution 2:

If you have the cron service installed you can use it for this purpose by using the @reboot time specification. What makes this easier to manage with puppet is that you can just create a separate file in /etc/cron.d instead of trying to edit a single monolithic file.

I tested that the following works:

file { "/etc/cron.d/at_startup_myscript":
    content => "@reboot root /usr/local/bin/myscript.sh \n",
}