Puppet 中的 validate_cmd:支持旧版本 [英] validate_cmd in Puppet: supporting older versions
问题描述
我有以下 Puppet 代码:
I have the following Puppet code:
file { "/etc/sudoers.d/${name}":
content => template('sudo/sudoers.erb'),
owner => 'root',
group => 'root'
mode => '0440',
validate_cmd => '/usr/sbin/visudo -c -f %',
}
不过,validate_cmd
仅在 Puppet > 3.5 中添加,并且我必须支持一些运行 3.2 及更早版本的系统.
However, validate_cmd
was only added in Puppet > 3.5, and I have to support some systems running 3.2 and older.
是否有一些聪明的方法可以将其修补到旧版本的 Puppet 中,或者我应该放弃使用 puppetlabs-stdlib
validate_cmd
语句?
Is there some clever way of monkey-patching this into older versions of Puppet, or should I just resign to using the puppetlabs-stdlib
validate_cmd
statement?
它做了类似的事情,但如果它没有正确验证就不会还原文件(文件中 validate_cmd 参数的主要好处).
Which does a similar thing, but won't revert the file if it does not validate correctly (the main benefit of the validate_cmd parameter on a fle).
使用 Felix 的覆盖语法,我得到了以下代码:
Using Felix's overwrite syntax, I ended up with the following code:
file { "/etc/sudoers.d/${name}":
content => template('sudo/sudoers.erb'),
owner => 'root',
group => 'root',
mode => '0440',
}
if versioncmp($::puppetversion, '3.5') >= 0 {
File["/etc/sudoers.d/${name}"] { validate_cmd => '/usr/sbin/visudo -c -f %' }
}
else {
validate_cmd(template('sudo/sudoers.erb'), '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content')
}
在 puppet-rspec 中测试有点繁琐,我最终选择了这个:
It's a bit fiddly to test in puppet-rspec, I ended up going with this:
if (Puppet.version >= '3.5.0')
context "validating content with puppet #{Puppet.version}" do
let(:params) { { :users => ['joe'] } }
let(:facts) {{ :puppetversion => Puppet.version }}
it { should contain_file('/etc/sudoers.d/worlddomination').with_validate_cmd('/usr/sbin/visudo -c -f %') }
end
else
context "validating content with puppet #{Puppet.version}" do
let(:params) { { :users => ['joe'] } }
let(:facts) {{ :puppetversion => Puppet.version }}
it { should contain_file('/etc/sudoers.d/worlddomination').with_validate_cmd(nil) }
end
end
推荐答案
您的清单可以根据代理版本调整其行为.
Your manifest can adjust its behavior to the agent version.
file { "/etc/sudoers.d/${name}":
content => template('sudo/sudoers.erb'),
owner => 'root',
group => 'root'
mode => '0440',
}
if versioncmp($puppetversion, '3.5') >= 0 {
File["/etc/sudoers.d/${name}"] { validate_cmd => '/usr/sbin/visudo -c -f %' }
}
else {
# your workaround here
}
当然,这将得益于 puppetversion
事实.
This will work courtesy of the puppetversion
fact, of course.
覆盖语法 File[
可以像这样使用,因为实际的资源声明没有指定 validate_cmd
属性的值.
The override syntax File[<name>] { ... }
can be used like this because the actual resource declaration specifies no value for the validate_cmd
attribute.
这篇关于Puppet 中的 validate_cmd:支持旧版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!