Ansible: Insert line if not exists

后端 未结 10 1321
庸人自扰
庸人自扰 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条回答
  •  猫巷女王i
    2020-12-25 11:40

    Ok, here is mine naive solution... probably not a cross-platform and native Ansible (I've just started to use this tool and still learn it), but definitely shorter:

    - name: Update /path/to/some/file
      shell: grep -q 'regex' /path/to/some/file && echo exists || echo 'text-to-append' >> /path/to/some/file
      register: result
      changed_when: result.stdout.find('exists') == -1
    

提交回复
热议问题