add 2 lines with lineinfile but only if not already there

22 views
Skip to first unread message

Cevyn E

unread,
Mar 25, 2015, 12:13:43 PM3/25/15
to ansible...@googlegroups.com
I'm new to Ansible and learning a lot but I have a stupid question.  I just can not quite see the answer.
I have a file

aaa
bbb
ccc

I want to add 
ddd
eee

but only if it does not already exist.  So I can rerun playbook without creating dups.

I've tried register and when.
I have tried using  backrefs=yes

It is just slipping though my fingers.  I'm sure this is child's play for many of you .  Please instruct...

J-P Methot

unread,
Mar 25, 2015, 12:54:55 PM3/25/15
to ansible...@googlegroups.com
You would need to use regexp. So, if you are trying to write ddd, the defined regexp would be :

regexp:'^ddd'

This way lineinfile will look for a line matching that regexp before writting. If it finds that line, it will get overwritten with the same content. Else, it will be added. state = present also needs to be specified.
Reply all
Reply to author
Forward
0 new messages