Using lineinfile to append items to the END of a line

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

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

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.

`

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:

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