【问题标题】:Ansible replace playbookAnsible 替换剧本
【发布时间】:2018-06-04 07:57:42
【问题描述】:

我最近开始学习 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 取代了我所有需要的东西,但我收到了这个警报。

[警告]:考虑使用替换、lineinfile 或模板模块 而不是运行 sed。如果您需要使用命令,因为替换, lineinfile 或模板不足,您可以在其中添加 warn=False 命令任务或在 ansible.cfg 中设置 command_warnings=False 以摆脱 这条消息。

那么,你能告诉我请纠正剧本吗?

【问题讨论】:

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

标签: replace ansible


【解决方案1】:

您应该首先阅读有关警告建议的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)'

【讨论】:

    猜你喜欢
    • 2020-05-08
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2022-10-24
    • 2023-01-09
    相关资源
    最近更新 更多