How to make a puppet copy a file only if the source exists?

I am trying to provide a vagrant VM so that users can provide their own bash_profile.local, but I do not want this file to be tracked in vm vcs repo. I have a bash_profile.local.dist tracked file that they can rename. How can I tell the puppet to only create the file if the original file exists? Currently it works correctly, but it logs an error during preparation, and this I am trying to avoid.

This is the manifest:

class local { file { '.bash_profile.local': source => 'puppet:///modules/local/bash_profile.local', path => '/home/vagrant/.bash_profile.local', replace => false, mode => 0644, owner => 'vagrant', group => 'vagrant', } } 
+7
vagrant puppet
source share
3 answers

You can abuse the file this way:

 $a = file('/etc/puppet/modules/local/files/bash_profile.local','/dev/null') if($a != '') { file { '.bash_profile.local': content => $a, ... } } 
+10
source share

This is not quite what you requested, but you can provide several paths in the source, so you may have an empty default file if the user has not provided their own.

 class local { file { '.bash_profile.local': source => [ 'puppet:///modules/local/bash_profile.local', 'puppet:///modules/local/bash_profile.local.default' ], path => '/home/vagrant/.bash_profile.local', replace => false, mode => 0644, owner => 'vagrant', group => 'vagrant', } } 
+4
source share

You can try something like this:

 file { 'bash_profile.local': ensure => present, source => ['puppet:///modules/local/bash_profile.local', '/dev/null'], path => '/home/vagrant/.bash_profile.local', before => Exec['clean-useless-file'], } exec { 'clean-useless-file': command => 'rm .bash_profile.local', onlyif => 'test -s .bash_profile.local', cwd => '/home/vagrant', path => '/bin:/usr/bin', } 

If the administrator does not create a copy of ".bash_profile.local" available in "modules / local / bash_profile.local", the file resource will use the second source and then create an empty file. Then the "onlyif" test fails, and exec will delete the useless empty file.

Used in this way, this code can be a little cumbersome, but it is better than refusing to initialize. You can evaluate if an empty .bash_profile.local file can be saved in your case. I usually use a variant of this, with wget instead of rm, to get a new copy of the file from the Internet, if it was not already available as a source.

If you are using puppetmaster, keep in mind that you can use it to provide your own server representing two versions of the directory, depending on .bash_profile.local.

0
source share

All Articles