Ansible: Insert line if not exists

后端 未结 10 1214
庸人自扰
庸人自扰 2020-12-25 11:27

I\'m trying insert a line in a property file using ansible. I want to add some property if it does not exist, but not replace it if such property already exists in the file.

10条回答
  •  礼貌的吻别
    2020-12-25 11:47

    Looks like it does not work if you use backrefs.

    Following sample does not add a line

    - name: add hosts file entry
      lineinfile:
        path: "/etc/hosts"
        regexp: "foohost"
        line:  "10.10.10.10 foohost"
        state: present
        backrefs: yes
    

    After removing backrefs I got my line added to the file

    - name: add hosts file entry
      lineinfile:
        path: "/etc/hosts"
        regexp: "foohost"
        line:  "10.10.10.10 foohost"
        state: present
    

提交回复
热议问题