You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Puppet Augeas资源多onlyif数组条件失效问题求助

Puppet Augeas资源修改/etc/fstab时onlyif数组条件不生效问题

我尝试用Puppet的Augeas资源修改/etc/fstab,给目标文件系统添加挂载选项,但要求两个条件同时满足才执行:

  • 该挂载选项尚未存在于目标文件系统的配置中
  • 目标文件系统已经在fstab里配置过

我配置了onlyif测试数组来实现这个逻辑,代码如下:

augeas { "fstab-${fsname}-${setting}":
  context => '/files',
  lens    => 'Fstab.lns',
  incl    => '/etc/fstab',
  onlyif  => [
    "values etc/fstab/*[file='${fsname}']/opt not_include ${setting}",
    "match etc/fstab/*[file='${fsname}'] size > 0",
  ],
  changes => [
    "insert opt after etc/fstab/*[file='${fsname}']/opt[last()]",
    "set etc/fstab/*[file='${fsname}']/opt[last()] ${setting}"
  ]
}

现象分析

  • 当目标文件系统存在时,资源能正确工作:选项不存在则添加,已存在则不执行变更,这部分符合预期。
  • 但当目标文件系统不在fstab中时,资源仍会尝试执行变更操作,因为找不到匹配项,导致insert命令报错。

我用augtool验证了匹配情况:

augtool> ls /files/etc/fstab/*[file='/var/tmp']
augtool> ls /files/etc/fstab/*[file='/tmp']
spec = /dev/mapper/vg_OS-lv_tmp
file = /tmp
vfstype = xfs
opt[1] = defaults
opt[2] = noexec
dump = 0
passno = 0
augtool>

从Puppet调试日志可以看到,onlyif测试没有被执行的记录,也没有语法错误,但直接执行了insert命令:

Debug: Augeas[fstab-/var/tmp-noexec](provider=augeas): Opening augeas with root /, lens path /opt/puppetlabs/puppet/cache/lib/augeas/lenses, flags 64
Debug: Augeas[fstab-/var/tmp-noexec](provider=augeas): Augeas version 1.12.0 is installed
Debug: Augeas[fstab-/var/tmp-noexec](provider=augeas): Will attempt to save and only run if files changed
Debug: Augeas[fstab-/var/tmp-noexec](provider=augeas): sending command 'insert' with params ["opt", "after", "/files/etc/fstab/*[file='/var/tmp']/opt[last()]"]
Debug: Augeas[fstab-/var/tmp-noexec](provider=augeas): Closed the augeas connection
Error: /Stage[main]/Smx_os_hardening::Filesystem/Augeas[fstab-/var/tmp-noexec]: Could not evaluate: Error sending command 'insert' with params ["opt", "after", "/files/etc/fstab/*[file='/var/tmp']/opt[last()]"]/Error sending command 'insert' with params ["opt", "after", "/files/etc/fstab/*[file='/var/tmp']/opt[last()]"]

Puppet文档说明onlyif支持单个测试或数组,数组条件是逻辑AND(全部为真才执行),但实际行为不符。另外我注意到Exec资源中使用onlyif数组是正常工作的,所以怀疑Augeas资源不支持onlyif数组。

请问我的操作哪里有误?使用的Puppet服务器和代理版本都是6.18。


内容的提问来源于stack exchange,提问作者Steve Shipway

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 03:25:26