Can ansible iterate through lines in a file?

1,444 views
Skip to first unread message

John Oliver

unread,
Aug 1, 2014, 1:47:14 PM8/1/14
to ansible...@googlegroups.com
I have a list of lines I want to add to a file on the target system if they don't exist.  I don't want to wholesale overwrite the file, and I don't want to write dozens of lineinfile: statements.

Is it possible to do the equivalent of:

while read line; do
  if grep "$line" /target/file >/dev/null 2>&1 ; then : ; else echo $line >> /target/file; fi
done < /source/file

Amr Ali

unread,
Aug 1, 2014, 2:33:36 PM8/1/14
to ansible...@googlegroups.com
You can use lineinfile and with_items

Ex:

- name: Add lines
lineinfile: dest=/some/file line={{ item }}
with_items:
- "add a line"
- "another line"
- "and so on"

Mike Ray

unread,
Aug 1, 2014, 2:35:00 PM8/1/14
to ansible...@googlegroups.com
I've also run into this issue and haven't picked the best way to deal with it yet.

You can always run that command via the shell module. Occasionally commands get a little gross and you'll have to add so many escape characters and quotes that even you can't tell what's going on. If that happens, you could consider adding the command as a file or template, pushing that to a host, running it and then deleting it (if you so choose). 

All of those are a little clunky and feel pretty anti-Ansible. It's goal is to be idempotent and with a command like that, you are breaking this canon. 

Out of curiosity, what are you trying to add to what?

John Oliver

unread,
Aug 1, 2014, 3:41:11 PM8/1/14
to ansible...@googlegroups.com
Dozens of lines to the audit log configuration.

I probably will just push a script and run it.  But you're right... it's "un-ansible-like" :-)

But so long as our scripts can be run idempotently, it isn't a total hack :-)

John Oliver

unread,
Aug 1, 2014, 3:46:13 PM8/1/14
to ansible...@googlegroups.com
If you just call lineinfile, where will it write them?  At the end?  What if your target file has some kind of terminator, can you do a blanket insertbefore or insertafter?  I'm not really grokking the concept of 'backrefs'.  

Outsider

unread,
Aug 1, 2014, 4:00:52 PM8/1/14
to ansible...@googlegroups.com

Calling lineinfile without any other arguments like my example would insert the lines at the end of the file if they don't already exist.

You can modify this behavior using insertbefore and insertafter, both take regex as a value, so you can determine certain sections to add the new lines  to.. using  maybe existing lines in the file or maybe placeholder comments?

Backrefs are used to replace specific parts of an existing line using the regex + line values.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/45qZWxkte3A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8a411626-485a-478a-805a-bded06019e36%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages