Ansible: Insert line if not exists

后端 未结 10 1303
庸人自扰
庸人自扰 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:54

    By a long way of "Trials and errors" I come to this:

    - name: check existence of line in the target file
      command: grep -Fxq "ip addr add {{ item }}/32 dev lo label lo:{{ app | default('app') }}" /etc/rc.local
      changed_when: false
      failed_when: false
      register: ip_test
      with_items:
        - "{{ list_of_ips }}"
    
    - name: add autostart command
      lineinfile: dest=/etc/rc.local 
                  line="ip addr add {{ item.item }}/32 dev lo label lo:{{ app | default('app') }}"
                  insertbefore="exit 0"
                  state=present
      when: item.rc == 1
      with_items:
        - "{{ ip_test.results }}"
    

提交回复
热议问题