Lineinfile Idempotency issue

62 views
Skip to first unread message

GANA SAGAR

unread,
Jul 23, 2016, 9:27:51 AM7/23/16
to Ansible Project
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





Kai Stian Olstad

unread,
Jul 23, 2016, 10:03:35 AM7/23/16
to ansible...@googlegroups.com
On 22. juli 2016 19:23, GANA SAGAR wrote:
> 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.

--
Kai Stian Olstad

GANA SAGAR

unread,
Jul 25, 2016, 4:09:02 PM7/25/16
to Ansible Project, ansible-pr...@olstad.com
Thanks very much Mr Olstad :)
Reply all
Reply to author
Forward
0 new messages