AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 554092
Accepted
Tombart
Tombart
Asked: 2013-11-12 01:29:30 +0800 CST2013-11-12 01:29:30 +0800 CST 2013-11-12 01:29:30 +0800 CST

如何用 puppet 更新 grub?

  • 772

我想/etc/default/grub用 puppet 把一行改成这样:

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'],
  }

它似乎有效,但结果字符串不在引号中,而且我想确保任何其他值都将用空格分隔。

GRUB_CMDLINE_LINUX=cgroup_enable=memory

是否有一些机制如何附加值并转义整个事情?

GRUB_CMDLINE_LINUX="quiet splash cgroup_enable=memory"
puppet
  • 2 2 个回答
  • 4266 Views

2 个回答

  • Voted
  1. Best Answer
    raphink
    2013-11-24T14:31:38+08:002013-11-24T14:31:38+08:00

    对于引用部分,您可以使用augeasproviders'shellvar提供者并强制引用样式:

    shellvar { 'GRUB_CMDLINE_LINUX':
      ensure   => present,
      target   => '/etc/default/grub',
      value    => 'cgroup_enable=memory',
      quoted   => 'double',
    }
    

    这将在代理上使用 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
    

    这会将事实的当前值作为字符串返回,并删除该值周围的引号。它需要代理上的 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,
    }
    

    运行

    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
    

    第二种选择

    如果您想尝试第二个选项,最好的方法可能是发送一个(不错的)PR 到augeasproviders'sshellvar类型添加一个array_append参数(或更好的名称):

    shellvar { 'GRUB_CMDLINE_LINUX':
      ensure       => present,
      target       => '/etc/default/grub',
      value        => 'cgroup_enable=memory',
      array_append => true,
      array_type   => 'double',
    }
    

    此参数会将值视为数组,如果尚未找到该值,则附加到现有值。这需要 Ruby 编码,但在许多其他情况下可以重用;-)

    提示:这应该发生在这里。

    • 7
  2. 131
    2016-02-04T15:52:53+08:002016-02-04T15:52:53+08:00

    (仅适用于 ubuntu 用户)

    因为改变 /etc/default/grub 不是很好..

    只需部署 /etc/default/grub.d/memory.cfg

    GRUB_CMDLINE_LINUX="$GRUB_CMDLINE_LINUX cgroup_enable=memory"

    然后更新-grub

    (请参阅 /etc/default/grub.d/*.cfg 功能)

    • 1

相关问题

  • 如何修复 Puppet 完全限定的参数路径错误?

  • puppet:修改配置文件后强制重启服务

  • 木偶模块资源

  • 傀儡主机名问题

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve