Tom*_*art 4 configuration-management puppet
我想将/etc/default/grub
puppet 中的一行更改为:
GRUB_CMDLINE_LINUX="cgroup_enable=memory"
Run Code Online (Sandbox Code Playgroud)
我试过使用 augeas 这似乎有这个魔法:
exec { "update_grub":
command => "update-grub",
refreshonly => true,
}
augeas { "grub-serial":
context => "/files/etc/default/grub",
changes => [
"set /files/etc/default/grub/GRUB_CMDLINE_LINUX[last()] cgroup_enable=memory",
],
notify => Exec['update_grub'],
}
Run Code Online (Sandbox Code Playgroud)
它似乎有效,但结果字符串不在引号中,而且我想确保任何其他值都用空格分隔。
GRUB_CMDLINE_LINUX=cgroup_enable=memory
Run Code Online (Sandbox Code Playgroud)
是否有某种机制如何附加值并逃避整个事情?
GRUB_CMDLINE_LINUX="quiet splash cgroup_enable=memory"
Run Code Online (Sandbox Code Playgroud)
对于引用部分,您可以使用augeasproviders
's shellvar
provider并强制引用样式:
shellvar { 'GRUB_CMDLINE_LINUX':
ensure => present,
target => '/etc/default/grub',
value => 'cgroup_enable=memory',
quoted => 'double',
}
Run Code Online (Sandbox Code Playgroud)
这将在代理上使用 Augeas internaly(作为 Ruby 库),只是以更智能的方式。
至于附加到现有值,有两个选项:
augeasfacter
用于检索它),在清单中分析它并使用shellvar
类型附加到它;shellvar
提供程序,使其附加到值而不是替换它。以下文件可以分布在您的模块中${modulepath}/${modulename}/lib/facter/grub_cmdline_linux.rb
并使用pluginsync
.
require 'augeas'
Facter.add(:grub_cmdline_linux) do
setcode do
Augeas.open(nil, '/', Augeas::NO_MODL_AUTOLOAD) do |aug|
aug.transform(
:lens => 'Shellvars.lns',
:name => 'Grub',
:incl => '/etc/default/grub',
:excl => []
)
aug.load!
aug.get('/files/etc/default/grub/GRUB_CMDLINE_LINUX').gsub(/['"]/, '')
end
end
end
Run Code Online (Sandbox Code Playgroud)
这将以字符串形式返回事实的当前值,并删除值周围的引号。它需要代理上的 Augeas Ruby 库,如果您augeas
已经使用该类型,我想您已经拥有了。
下一步是利用此值来检查是否包括您的目标值。您可以拆分字符串并为此使用stlib
模块函数:
$value = 'cgroup_enable=memory'
# Split string into an array
$grub_cmdline_linux_array = split($::grub_cmdline_linux, ' ')
# Check if $value is already there to determine new target value
$target = member($grub_cmdline_linux_array, $value) ? {
true => $grub_cmdline_linux_array,
false => flatten([$grub_cmdline_linux_array, $value]),
}
# Enforce new target value
# Use the array and force the 'string' array type
shellvar { 'GRUB_CMDLINE_LINUX':
ensure => present,
target => '/etc/default/grub',
value => $target,
array_type => string,
}
Run Code Online (Sandbox Code Playgroud)
Notice: /Stage[main]//Shellvar[GRUB_CMDLINE_LINUX]/value: value changed ['quiet splash usbcore.old_scheme_first=1'] to 'quiet splash usbcore.old_scheme_first=1 cgroup_enable=memory'
Run Code Online (Sandbox Code Playgroud)
检查幂等性:
Notice: Finished catalog run in 0.17 seconds
Run Code Online (Sandbox Code Playgroud)
如果您想尝试第二个选项,最好的方法可能是发送一个(不错的)PR 到augeasproviders
's shellvar
type 添加一个array_append
参数(或更好的名称):
Notice: /Stage[main]//Shellvar[GRUB_CMDLINE_LINUX]/value: value changed ['quiet splash usbcore.old_scheme_first=1'] to 'quiet splash usbcore.old_scheme_first=1 cgroup_enable=memory'
Run Code Online (Sandbox Code Playgroud)
此参数会将值视为数组,如果尚未找到该值,则附加到现有值。这将需要 Ruby 编码,但可以在许多其他情况下重用 ;-)
提示:这应该发生在这里。