Ansible: Insert line if not exists

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

    This is possible by simply using lineinfile and check_mode:

    - name: Check if couchbase.host is already defined
      lineinfile:
        state: absent
        path: "/database.properties"
        regexp: "^couchbase.host="
      check_mode: true
      changed_when: false # This just makes things look prettier in the logs
      register: check
    
    - name: Define couchbase.host if undefined
      lineinfile:
        state: present
        path: "/database.properties"
        line: "couchbase.host=127.0.0.1"
      when: check.found == 0
    
    

提交回复
热议问题