2014-02-15 23 views
5

以下任務總是觸發通知爲什麼這個ansible lineinfile任務總是觸發一個通知?

第一次運行時,應用預期的更改, 和行被更改。 如果我再次運行它,ansible認爲它是「改變」,儘管 正則表達式不可能匹配,因爲行成了 「綁定地址= 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 

回答

13

參考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