puppet: if one file exists then copy another file

2019-09-02 07:06发布

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!

2条回答
等我变得足够好
2楼-- · 2019-09-02 07:21

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"],
 }
}
查看更多
别忘想泡老子
3楼-- · 2019-09-02 07:38

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'],
}
查看更多
登录 后发表回答