# lineinfile when two lines are the same

**URL:** <https://forum.ansible.com/t/lineinfile-when-two-lines-are-the-same/17865>\
**Category:** Ansible Project\
**Created:** [March 29, 2015, 4:34am UTC](https://forum.ansible.com/t/lineinfile-when-two-lines-are-the-same/17865 "2015-03-29T04:34:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vince\_Skahan](https://avatars.discourse-cdn.com/v4/letter/v/5e9695/32.png) [@Vince\_Skahan](https://forum.ansible.com/u/Vince_Skahan)\
**Post date:** [March 29, 2015, 4:34am UTC](https://forum.ansible.com/t/lineinfile-when-two-lines-are-the-same/17865/1 "2015-03-29T04:34:20Z")

</div>

I’m installing a piece of software that requires me to edit a user=xyz line into a monolithic config file that is .ini like in format.

> [function-x]

> # user=myuserhere

> [function-y]
> 
> # other stuff here
> 
> # more stuff here

> [function-z]
> 
> # user=myuserhere

> 

> 

So suppose I have a config that requires me to uncomment the line in [function-Z] and substitute in the right value for the username.

How do you specify which of the two identical lines in the file that you want to edit ?  
Any suggestions ?

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [March 29, 2015, 7:33pm UTC](https://forum.ansible.com/t/lineinfile-when-two-lines-are-the-same/17865/2 "2015-03-29T19:33:40Z")

</div>

Would it be acceptable to template that config file? Perhaps save that chunk only, in a templated file that would be later on included.

---

<div class="post-metadata">

**Author:** ![Adam\_Herzog](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Adam\_Herzog](https://forum.ansible.com/u/Adam_Herzog)\
**Post date:** [March 29, 2015, 7:41pm UTC](https://forum.ansible.com/t/lineinfile-when-two-lines-are-the-same/17865/3 "2015-03-29T19:41:03Z")

</div>

Try the ini\_file module ([http://docs.ansible.com/ini\_file\_module.html](http://docs.ansible.com/ini_file_module.html)) instead of lineinfile.

You want something like this, I think:

- ini\_file:  
&nbsp;&nbsp;dest=/etc/anotherconf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;section=function-z  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;option=user  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value=myuserhere  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backup=yes

-A
