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

时间:2017-03-17 20:20:03

标签: 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:   "local   all             postgres                                trust"
....

我想用信任

替换 peer postgres配置中的

。当我第一次运行剧本时,它按预期工作正常。但是当我运行它2次或更多次时,剧本开始在文件的底部附加一个新行,因为搜索字符串不再是假的。如何防止ansible这样做?

1 个答案:

答案 0 :(得分:2)

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

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

来自:lineinfile

<强> backrefs

  

如果正则表达式不匹配文件中的任何位置,则文件将为   保持不变。如果正则表达式匹配,则最后一个匹配行将   被扩展的行参数替换。