[英]Ansible replace playbook
我最近開始學習Ansible,現在我有一個疑問:
這是我的劇本:
---
- name: execute command with sudo
hosts: all
user: root
become: yes
tasks:
- name: executing
command: sed -i "s/print_thresholds($name,undef,undef,92,98);/#print_thresholds($name,undef,undef,92,98);/g" /etc/munin/plugins/df
它工作正常,sed代替了我所有我需要的東西,但我收到了此警報。
[警告]:考慮使用replace,lineinfile或模板模塊,而不是運行sed。 如果由於替換,行文件或模板不足而需要使用命令,則可以向該命令任務添加warn = False或在ansible.cfg中設置command_warnings = False來擺脫此消息。
所以,你能告訴我請更正劇本嗎?
您應該首先閱讀有關lineinfile模塊的信息,這是警告提示。
然后,盡管您的代碼和sed的使用是正確的,但這不是使用Ansible編輯文件的最安全方法,主要是因為它不具有冪等性,並且與使用Ansible模塊一樣具有抗錯誤性。
該代碼產生與sed命令相同(在某些情況下更好)的結果,但使用lineinfile模塊:
- name: executing
lineinfile:
path: /etc/munin/plugins/df
regexp: 'print_thresholds($name,undef,undef,92,98)'
line: '^#print_thresholds($name,undef,undef,92,98)'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.