问题
I am trying to figure out how to make my puppet module work such I need to test if file exists on the client, if it does, then copy another file over. If the file does not exist then do nothing. I can't seem to get it working. Here is my module:
class web-logs::config {
# PATH TO LOG FILES
$passenger='/var/tmp/puppet_test/passenger'
# PATH TO LOGROTATE CONFIGS
$passenger_logrotate='/var/tmp/puppet_test/logrotate.d/passenger'
exec { 'test1':
onlyif => "test -f $passenger",
path => ['/usr/bin','/usr/sbin','/bin','/sbin'],
refreshonly => true,
} ~>
exec { 'test2':
require => Class['web-logs::passenger']
}
And the Class['web-logs::passenger'] looks like this:
class web-logs::passenger {
file { 'passenger':
path => '/var/tmp/puppet_test/logrotate.d/passenger',
owner => 'root',
group => 'root',
mode => '0644',
source => "puppet://${puppetserver}/modules/web-logs/passenger.conf",
}
}
Any help would be appreciated thanks!
回答1:
The exec is failing since you are missing the command to execute. Right now everything fails because of the failing exec requirement in the file resource. This one should do the trick:
exec { 'test1':
command => "/bin/true",
onlyif => "test -f $passenger",
path => ['/usr/bin','/usr/sbin','/bin','/sbin'],
}
# Check if passenger file exists then push logrotate module for passenger
file { 'passenger':
path => '/var/tmp/puppet_test/logrotate.d/passenger',
owner => 'root',
group => 'root',
mode => '0644',
source => "puppet://${puppetserver}/modules/web-logs/passenger.conf",
require => Exec["test1"],
}
If you get disturbed by the message that the command has been successfully executed on each run you could try to modify the exec like this
exec { 'test1':
command => "/bin/false",
unless => "test -f $passenger",
path => ['/usr/bin','/usr/sbin','/bin','/sbin'],
}
回答2:
Remove exec test2
. It isn't necessary. You need to require
the exec test1
in the file passenger
. Something like this :
class web-logs::passenger {
file { 'passenger':
path => '/var/tmp/puppet_test/logrotate.d/passenger',
owner => 'root',
group => 'root',
mode => '0644',
source => "puppet://${puppetserver}/modules/web-logs/passenger.conf",
require => Exec["test1"],
}
}
来源:https://stackoverflow.com/questions/31463506/puppet-if-one-file-exists-then-copy-another-file-over