# Using lineinfile to append items to the END of a line

**URL:** <https://forum.ansible.com/t/using-lineinfile-to-append-items-to-the-end-of-a-line/31259>\
**Category:** Ansible Project\
**Created:** [September 11, 2019, 12:45pm UTC](https://forum.ansible.com/t/using-lineinfile-to-append-items-to-the-end-of-a-line/31259 "2019-09-11T12:45:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheSmiths](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@TheSmiths](https://forum.ansible.com/u/TheSmiths)\
**Post date:** [September 11, 2019, 12:45pm UTC](https://forum.ansible.com/t/using-lineinfile-to-append-items-to-the-end-of-a-line/31259/1 "2019-09-11T12:45:04Z")

</div>

Hi,

I would like to use lineinfile module to do the next:

1. go over list of items (with\_items)
2. look for specific line that always starts with the word “ALL:” (hosts.allow)
3. append each item at the END of the line with comma before the item

for example

ALL ,1.1.1.1/255.255.255.255 , 2.2.2.2/255.255.255.255

Thanks,  
Morrissey

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [September 12, 2019, 5:26am UTC](https://forum.ansible.com/t/using-lineinfile-to-append-items-to-the-end-of-a-line/31259/2 "2019-09-12T05:26:11Z")

</div>

I would recommend the Template module to do it

while technically possible with lineinfile, the regular expression to do it will be overly complex  
and hard to read/maintain

---

<div class="post-metadata">

**Author:** ![shivharsh.nec](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@shivharsh.nec](https://forum.ansible.com/u/shivharsh.nec)\
**Post date:** [September 12, 2019, 7:15am UTC](https://forum.ansible.com/t/using-lineinfile-to-append-items-to-the-end-of-a-line/31259/3 "2019-09-12T07:15:24Z")

</div>

Hello Morrissey,

You may use the below playbook to fulfill your requirements. It makes use of lineinfile module with regular expression to capture the line to be modified.

`

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 12, 2019, 10:27am UTC](https://forum.ansible.com/t/using-lineinfile-to-append-items-to-the-end-of-a-line/31259/4 "2019-09-12T10:27:21Z")

</div>

This will work, but it's not idempotent.  
So if you run it again it will add the two CIDRs again.  
For it to work idempotently, the regex will need to match both what  
you have before and what you get after.  
Something like this should work:

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [September 12, 2019, 2:08pm UTC](https://forum.ansible.com/t/using-lineinfile-to-append-items-to-the-end-of-a-line/31259/5 "2019-09-12T14:08:12Z")

</div>

not idempotent when the list ordering change, plus some others

for each item in the list you have to add it if not already there (requires a look-ahead expression in regex)  
that is what make it quite complex at the end, but doable as long as  
per the spec the only need is to append to an existing list.

hence the idea to leverage the template module  
that also can deal with removal of item from the list
