我想用puppet把/etc/default/grub的一行改为:
GRUB_CMDLINE_LINUX="cgroup_enable=memory"
我试图用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'], }
它似乎工作,但结果string不引号,也我想确保任何其他值将被空格分隔。
GRUB_CMDLINE_LINUX=cgroup_enable=memory
是否有一些机制如何附加值和逃避整个事情?
GRUB_CMDLINE_LINUX="quiet splash cgroup_enable=memory"
对于引用部分,您可以使用augeasproviders的shellvar提供程序并强制引用样式:
shellvar { 'GRUB_CMDLINE_LINUX': ensure => present, target => '/etc/default/grub', value => 'cgroup_enable=memory', quoted => 'double', }
这将在代理上使用Augeas内部(作为一个Ruby库),只有一个更聪明的方式。
至于追加现有的价值,有两个select:
augeasfacter来检索),在清单中分析它并使用shellvartypes追加到它; shellvar提供程序,使其附加到值而不是replace它。 以下文件可以分布在${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
这将以string的forms返回事实的当前值,并删除该值周围的引号。 它需要代理上的Augeas Ruby库,如果你已经使用了augeastypes的话,我想你应该有这个augeas 。
下一步是利用这个值来检查你的目标值是否包含在内。 您可以拆分string,并使用stlib模块function:
$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, }
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'
检查幂等性:
Notice: Finished catalog run in 0.17 seconds
如果你希望在第二个选项中augeasproviders ,最好的办法可能是发送一个(好的)PR到augeasproviders者的shellvartypes,并添加一个array_append参数(或者一个更好的名字):
shellvar { 'GRUB_CMDLINE_LINUX': ensure => present, target => '/etc/default/grub', value => 'cgroup_enable=memory', array_append => true, array_type => 'double', }
这个参数将把这个值当作一个数组来处理,如果这个值还没有被find,那么会追加到现有的值上。 这将需要Ruby编码,但可以在许多其他情况下重用;-)
提示:这应该发生在这里 。
(仅适用于ubuntu用户)
正如改变/ etc / default / grub不是那么好..
只需部署/etc/default/grub.d/memory.cfg
GRUB_CMDLINE_LINUX="$GRUB_CMDLINE_LINUX cgroup_enable=memory"
然后更新grub
( 请参阅/etc/default/grub.d/*.cfgfunction )