2016-11-24 32 views
0

我正在尋找一個強大的方式來編輯現有的配置文件與ansible。部分知道編輯配置文件沒有複雜的正則表達式

在下面的例子我想改變的key值在[right_section]或添加它,如果它根本不存在吧:

[wrong_section] 
key = value 

[right_section] 
wrong_key = value 
# key = value 
key = wrong_value 
wrong_key = value 

# [right_section] 
# key = value 

我想不出一個好辦法一起做lineinfile或替換而不使用過於複雜的正則表達式。

回答

1

ini_file模塊。從文檔

例子:

# Ensure "fav=lemonade is in section "[drinks]" in specified file 
- ini_file: dest=/etc/conf section=drinks option=fav value=lemonade mode=0600 backup=yes 
+0

謝謝!恥辱我沒有找到它自己,並在SO上錯過了這個問題:http://stackoverflow.com/questions/22339832/which-is-the-best-way-to-make-config-changes-in-conf- files-in-ansible/22342331#22342331 我認爲我的問題應該標記爲重複。 – Jonatan