LineInFile: How to replace value in a line (or append if missing)
My limited regex has failed me. I am guessing I need a look-around of some kind but I have not reached that level yet.
I want to replace a value in a line if it exists, and append the value to that line if it does not. (This is a one-line file).
Here is what I have so far:
tasks:
- name: Enable Screen Timeout
lineinfile:
path: /boot/cmdline.txt.bak
regexp: '^(.*?\\s)?(consoleblank=\\d*)(.*)$'
line: '\1consoleblank=900\3'
backrefs: true
become: yes
Test examples:
abc=123 d=4 w=6 xyz=789
abc=123 d=4 consoleblank=0
abc=123 d=4 consoleblank=0 w=6 xyz=789
consoleblank=0 w=6 xyz=789
Am I being too aggressive with this one task? Do I need to use multiple tasks (like a replace and a lineinfile)? Please advise. Thanks.
Posted Jan 30, 2020, 8:03 PM UTC
Look at the literal Replace module. I had issues with things like that. Replace did the trick.
But replace wouldn’t help if there isn’t already a value there. Right?
Empty space can be replaced with not empty space if your checking for it in your regex. It may be worth doing two replaces though. One checking for the existing value and one checking for the empty space. Either way only one triggers.
But it wouldn’t be empty. It wouldn’t be there in the list of values, but there wouldn’t be a “gap” of some kind. And I don’t see a way to use replace module when the regex is NOT met.
So for the first part, about replacing if present. Replace will work. That is easy and sets us up for the second part. The second part could likely be resolved by registered a variable during the first replace. Something like...
I just copied your example, so your check might actually need to be different just to replace the expected value.
Then to append if not present at all, go with something like lineinfile to replace the whole thing with what you want, based on if the first task actually changed anything or not.
The when statement will need to be tested and probably properly formatted, but the idea is there. We only want the second task to trigger when the first fails to change anything (i.e. Does not find the value to replace in the first place)
Sorry if this is a bit all over the place. Trying to write it up in between my kid wanting my full attention lol
I hope this makes sense and may help.