为什么这个ansible lineinfile任务总是触发通知?

时间:2014-02-15 07:50:05

标签: ansible ansible-playbook

以下任务始终触发通知

第一次运行ansible时会应用预期的更改, 并且线路已更改。 如果我再次运行它,ansible认为它“改变了”,即使 正则表达式不可能匹配,因为该行已成为 “bind-address = 0.0.0.0”

为什么?

  - name: Ensure MySQL will listen on all ip interfaces (bind to 0.0.0.0)
    lineinfile: dest=/etc/mysql/my.cnf
      regexp='bind-address\s*=\s*127\.0\.0\.1\s*'
      line='bind-address   = 0.0.0.0'
      state=present
      insertafter=EOF
    notify: restart mysql

1 个答案:

答案 0 :(得分:19)

请参阅lineinfile模块的the backrefs option。具体来说,“如果正则表达式与文件中的任何位置不匹配,则文件将保持不变。”工作游戏看起来像这样:

- name: Ensure MySQL will listen on all ip interfaces (bind to 0.0.0.0)
  lineinfile: dest=/etc/mysql/my.cnf
    regexp='bind-address\s*=\s*127\.0\.0\.1\s*'
    line='bind-address   = 0.0.0.0'
    state=present
    backrefs=yes
  notify: restart mysql
相关问题