puppet: if one file exists then copy another file over

我们两清 提交于 2019-12-11 22:07:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!