2017-05-22 26 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