# lineinfile tasks adds line when exists

**URL:** <https://forum.ansible.com/t/lineinfile-tasks-adds-line-when-exists/22079>\
**Category:** Ansible Project\
**Created:** [July 21, 2016, 6:30pm UTC](https://forum.ansible.com/t/lineinfile-tasks-adds-line-when-exists/22079 "2016-07-21T18:30:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Margeson](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@Robert\_Margeson](https://forum.ansible.com/u/Robert_Margeson)\
**Post date:** [July 21, 2016, 6:30pm UTC](https://forum.ansible.com/t/lineinfile-tasks-adds-line-when-exists/22079/1 "2016-07-21T18:30:56Z")

</div>

I wrote a playbook to make a config edit in /etc/postgreql/95/main/pg\_hba.conf.

Basically, it’s to search for an existing string and replace it.

Trouble is, when the playbook is ran for a second time, it adds the line as if it didn’t exist.

Thoughts?

- name: postgres | Allow MD5 authentication  
lineinfile: \>  
dest=/etc/postgresql/9.5/main/pg\_hba.conf  
regexp=“local\s+all\s+all\s+peer”  
line=“local {{database.name}} {{database.user}} md5”  
backup=yes  
state=present  
insertafter=yes

----pg\_hba.conf before running playbook

# Database administrative login by Unix domain socket

local all postgres peer

# TYPE DATABASE USER ADDRESS METHOD

# “local” is for Unix domain socket connections only

local all all peer \<---- to be changed

# IPv4 local connections:

host all all 127.0.0.1/32 md5

# IPv6 local connections:

host all all ::1/128 md5

# Allow replication connections from localhost, by a user with the

# replication privilege.

#local replication postgres peer  
#host replication postgres 127.0.0.1/32 md5  
#host replication postgres ::1/128 md5

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 21, 2016, 6:47pm UTC](https://forum.ansible.com/t/lineinfile-tasks-adds-line-when-exists/22079/2 "2016-07-21T18:47:15Z")

</div>

[https://github.com/ansible/ansible-modules-core/issues/3975](https://github.com/ansible/ansible-modules-core/issues/3975)

---

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [July 21, 2016, 7:12pm UTC](https://forum.ansible.com/t/lineinfile-tasks-adds-line-when-exists/22079/3 "2016-07-21T19:12:14Z")

</div>

Hi Robert,

To replace an existing line, you will want to use backrefs. Otherwise, I believe the default behavior is to insert the line after EOF or the last match of a specified regexp. Insertafter and backrefs are exclusive of each other, since the line will be added after, or replace an existing line, depending which you use.

So, maybe this would work (add backrefs and remove insertafter):

- name: postgres | Allow MD5 authentication  
lineinfile: \>  
dest=/etc/postgresql/9.5/main/pg\_hba.conf  
regexp=“local\s+all\s+all\s+peer”  
line=“local {{[database.name](http://database.name/)}} {{database.user}} md5”  
backup=yes  
backrefs=yes  
state=present

The trick now is that you have duplicates in your files, and I don’t yet have a great way to clean those up. Maybe a task with state=absent and then a task to add it back. I’m not entirely sure whether state=absent removes all matches, or just the last match, since I don’t use it much and it’s not documented.

Also, backrefs doesn’t work with create=yes. I discovered that unfortunate fact recently.

Joanna
