Lineinfile Idempotency issue

Is lineinfile really idempotent ?? I am using it to uncomment the logrorate option in logrotate.conf. Everytime I run the playbook. This play is being executed every single time and add the word compress at the bottom after every run. Am I doing something wrong ??

  • name: Enabling the logrotate option for linux servers
    lineinfile: dest=/etc/logrotate.conf
    backup=yes
    regexp=“^#compress”
    line=“compress”
    create=yes
    when: ansible_distribution == “RedHat”

Thanks in Advance
Sagar

Is lineinfile really idempotent ??

Yes and no, it depend on how you use it.

I am using it to uncomment the
logrorate option in logrotate.conf. Everytime I run the playbook. This play
is being executed every single time and add the word compress at the bottom
after every run. Am I doing something wrong ??

It is actually working as the documentation is describing.
If it can't find the regexp expression the line will be added to the file, default is EOF since insertafter take presidence and is default set to EOF.

Someone has created a issue about this behavior.
https://github.com/ansible/ansible-modules-core/issues/3975

  - name: Enabling the logrotate option for linux servers
    lineinfile: dest=/etc/logrotate.conf
                  backup=yes
                  regexp="^#compress"
                  line="compress"
                  create=yes
   when: ansible_distribution == "RedHat"

If you add backrefs=yes it will work as you expect, it disables insertafter.

Thanks very much Mr Olstad :slight_smile: