运行ansible lineinfile两次会在文件末尾添加一行

运行ansible lineinfile两次会在文件末尾添加一行,ansible,yaml,Ansible,Yaml,我有一本ansible playbook,我想更改postgres数据库的配置: ... - name: Configuring postgres become: yes lineinfile: dest: /etc/postgresql/9.5/main/pg_hba.conf regexp: "^local all postgres peer$" line: "loc

我有一本ansible playbook,我想更改postgres数据库的配置:

...
- name: Configuring postgres
  become: yes
  lineinfile:
    dest: /etc/postgresql/9.5/main/pg_hba.conf
    regexp: "^local   all             postgres                                peer$"
    line:   "local   all             postgres                                trust"
....
我想用信任取代同龄人


在postgres配置中。当我第一次运行playbook时,它工作正常。但当我运行它2次或更多次时,playbook开始在文件底部追加一行新行,因为搜索字符串不再为false。如何防止ansible执行此操作?

您必须添加
backrefs:yes
以使其幂等

  lineinfile:
    dest: /etc/postgresql/9.5/main/pg_hba.conf
    backrefs: yes
    regexp: "^local   all             postgres                                peer$"
    line:   "local   all             postgres                                trust"
发件人:

背景参考

如果regexp与文件中的任何位置不匹配,则文件将被删除 保持不变。如果regexp不匹配,则最后一个匹配行将被删除 将替换为展开的线参数