2017-05-22 31 views
2

我使用puppet 4.5.3和ini_setting模块版本1.4.2。我需要能够删除ini文件中的节。例如:Puppet - 完全删除ini_setting

[header] 
ip = '1.1.1.1' 
hostname = 'myserver' 
port = 80 

我能够删除使用ensure => absent参数ini文件中的每个部分,但我无法找到一个方法来删除节头,最好在整个事件中一个命令。

我已经离开是

[header] 

有谁知道如何可以做到这一点?不幸的是,在同一个文件中还有其他节,我需要保留,所以我不能简单地删除文件。

感谢,

+0

用augeas删除包含标头的大部分文件可能会更好:https://docs.puppet.com/puppet/4.10/resources_augeas.html。这看起来很吸引你吗? –

+0

这是我成功的最后一次尝试,然后我屈服于疯狂,这是augeas :( – jacksonecac

+0

我不特别关心augeas,但它可能是你最好的选择在这里。 –

回答

2

使用Augeas类型:

augeas { 'remove_ini_header': 
    incl => '/etc/example.ini', 
    lens => 'IniFile.lns_loose', 
    changes => 'rm section[. = "header"]', 
} 

要打破这一点,首先我用内置的IniFile.lns_loose镜头(即INI文件“通用”松解析)和augtool看到树的当前状态:

$ augtool -t "IniFile.lns_loose incl /etc/example.ini" 
augtool> print /files/etc/example.ini 
/files/etc/example.ini 
/files/etc/example.ini/section = "header" 
/files/etc/example.ini/section/ip = "'1.1.1.1'" 
/files/etc/example.ini/section/hostname = "'myserver'" 
/files/etc/example.ini/section/port = "80" 

整款是树中的一个组成部分,因此调用rm的塔t部分将删除整个子树。

要匹配标题部分,您需要搜索名为section的节点,其值(右侧)为header[. = "header"]命令的一部分是path expression,它针对值为header的节点进行过滤。

+0

作品像一个魅力。谢谢。 – jacksonecac