Search code examples
ansibleidempotent

Idempotency in ansible playbook


I am configuring a server using Ansible playbook. My playbook works properly at the very first execution but when I run the same playbook again it creates the duplicates lines in the configuration file on the server. I am using lineinfile module. Example following task adds the line each time I run the playbook.

- lineinfile: dest=/etc/pam_ldap.conf line="ssl off"

Is there a way to avoid this, and maintain idempotency.


Solution

  • Theoretically lineinfile should work as you expect it. A line only is added if it is not already present in the file.

    Is the file a symlink? I don't see a reason why Ansible shouldn't follow that link, but maybe that might be a reason why it fails to identify the line.

    Did you try to add a regexp parameter? It would make sense anyway, to cover cases where a line like ssl on already is present.

    - lineinfile: dest=/etc/pam_ldap.conf
                  line="ssl off"
                  regexp="^ssl\s+"