Augeas-创建新的 ini 部分

Augeas-创建新的 ini 部分

我在 augeas 中有一个配置文件,使用自定义镜头,输出数据如下。

/files/opt/../server.conf/target[1] = "general"
/files/opt/../server.conf/target[1]/serverName = "XXX"
/files/opt/../server.conf/target[1]/guid = "XXX0XXX"
/files/opt/../server.conf/target[2] = "sslConfig"
/files/opt/../server.conf/target[2]/sslKeysfilePassword  = "$1$XXXXX" 

这很有效 - 一些目标名称包含冒号等,所以我需要使用目标 [x] 格式。

使用此语法在我的 INI 中创建新部分的正确 ins 语法是什么?

答案1

您可以使用:

set /files/opt/.../server.conf/target[. = 'newsection'] 'newsection'

如果该部分尚不存在,它将创建它。然后您可以使用它在此部分下添加键。此代码是幂等的,因此可以安全地将其用于您需要键的每个部分,因为它只会在部分尚不存在时创建它。

为了记录在案,你可以ins如果您确实希望新部分出现在现有部分之前,请使用:

# Add a new target node before the sslConfig target
ins target before /files/opt/.../server.conf/target[. = 'sslConfig']
# Set the target name to "newsection" by matching the node right before the "sslConfig" target
set /files/opt/.../server.conf/target[following-sibling::*[1][label() = 'target'][. = 'sslConfig']] 'newsection'

请注意上面的代码是不是与我粘贴的第一段代码不同,它是幂等的。如果您将 Augeas 与 Puppet 一起使用,则可能需要添加一条onlyif语句以幂等方式应用此类更改。

相关内容