当某个配置文件发生更改时,在尝试重新启动服务之前,有没有办法运行 configtest 实用程序(例如 apache2ctl -t)?
这里的用例是,我想要对 apache 配置中的配置错误具有弹性(因为 apache 在看到配置错误时就会停止,导致 apache 停止运行)
答案1
您可以使用require => Exec[]
菊花链来exec
要求配置更改。
如果 Exec 失败(退出状态 != 0),则需要它的后续任务也应失败。我执行了类似 ssh-keygen 的操作,然后在创建新用户时对 .ssh 设置了特殊权限。
例子:
# Ensure the .ssh directory exists for each user
file { "${dot_ssh_dir}":
ensure => directory,
owner => $title,
mode => 700,
require => File["${home_dir}"],
}
# Run ssh-keygen (ONLY if id_rsa doesn't exist)
exec { "${title} ssh-keygen":
command => "ssh-keygen -f ./id_rsa -q -N ''",
cwd => $dot_ssh_dir,
user => $title,
creates => "${keyfile_private}",
path => ['/usr/bin', '/usr/sbin/', '/usr/local/sbin'],
require => File[$dot_ssh_dir],
}
# AuthorizedKeys should be 600
file { "${authorized_keys}":
source => "${keyfile_public}",
ensure => present,
mode => 600,
owner => $title,
group => 'sftpusers',
require => Exec["${title} ssh-keygen"],
}