Struggling with lineinfile behavior.
The general question: can lineinfile be used to replace a line in a file if it’s found and if it’s not found add the line to the file?
I’ve seen posts saying yes, same saying no, same saying don’t use lineinfile, use a template.
Here’s the recipe I’ve been trying:
lineinfile: >
dest=“/tmp/testfile”
regexp=“(.){{ item.param }}(.)?$”
line=‘{{ item.param }} {{ item.value }}’
backrefs=“yes”
state=“present”
with_items:
- { param: ‘testparam’, value: ‘testvalue’ }
The contents of /tmp/testfile
blah blahvalue
foo foovalue
ugh ughvalue
I run the task and “testparam testvalue” does not get added to /tmp/testfile
blah blahvalue
foo foovalue
ugh ughvalue
testvalue wrongvalue
Run the task again and I get what I think is the right output
blah blahvalue
foo foovalue
ugh ughvalue
testvalue testvalue