首页 > 解决方案 > 根据 Puppet 中文件的存在应用不同的配置

问题描述

如果另一个文件存在,我想在 Puppet 清单中添加一个功能来应用一个配置,如果不存在应该应用不同的配置。我已经尝试了这部分代码:

    exec { "chk_file_exist":
    path    =>  ["/usr/bin","/usr/sbin", "/bin"],
    onlyif  => "test -f ${file} && echo 0",
    before => File['/etc/systemd/system/config1.service'],
   }

   exec { "chk_file_doesnt_exist":
    path    =>  ["/usr/bin","/usr/sbin", "/bin"],
    onlyif  => "test ! -f ${file} && echo 0",
    refreshonly => true,
    before => File['/etc/systemd/system/config2.service'],
   }

   file { '/etc/systemd/system/config1.service':
       content => template('module/service/config1.service'),
       require => Exec["chk_file_exist"],
   }

   file { '/etc/systemd/system/config2.service':
       content => template('module/service/config2.service'),
       require => Exec["chk_file_doesnt_exist"],
      } 

它只创建一个文件,但会引发错误:

Error: Could not find command 'chk_file_exist'
Error: /Stage[main]/mymodule::service/Exec[chk_file_exist]/returns: change from 'notrun' to ['0'] failed: Could not find command 'chk_file_exist' (corrective)

我怎样才能避免这个错误?

谢谢

标签: fileexecpuppet

解决方案


您收到该错误是因为您没有为exec资源赋予command属性。资源默认使用exec其标题作为命令

尝试以下类似的操作,它true用作不执行任何操作的命令。

exec { "chk_file_exist":
  command => 'true',
  path    =>  ["/usr/bin","/usr/sbin", "/bin"],
  onlyif  => "test -f ${file} && echo 0",
  before  => File['/etc/systemd/system/config1.service'],
}

推荐阅读