Replace Ansible替换剧本

Replace Ansible替换剧本,replace,ansible,Replace,Ansible,我最近开始学习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,unde

我最近开始学习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。如果因为替换而需要使用命令, lineinfle或template不足您可以将warn=False添加到此 命令任务或在ansible.cfg中设置命令_warnings=False以清除 这条消息的一部分


那么,您能告诉我您的操作手册是否正确吗?

您应该首先阅读警告所建议的模块

然后,尽管您的代码和sed的使用是正确的,但使用Ansible编辑文件并不是最安全的方法,主要是因为它不像使用Ansible模块那样具有幂等性和容错性

此代码生成与sed命令相同(在某些情况下更好)的结果,但使用LINEINFLE模块:

- name: executing
  lineinfile:
    path: /etc/munin/plugins/df
    regexp: 'print_thresholds($name,undef,undef,92,98)'
    line: '^#print_thresholds($name,undef,undef,92,98)'

我们建议您阅读警告并使用其中列出的任何方法。在哪里列出?你能给我方法的链接吗?在警告信息中,你把自己添加到了上述问题中。