Question:
In the lineinfile module, it replaces the full line.
If the line is long I have to repeat the whole line again.
Let us suppose I want to replace the single word in the file:
1 2 3 |
#abc.conf This is my horse |
this is the playbook:
1 2 3 4 5 6 |
- lineinfile: dest=abc.conf state=present regexp='horse' line='This is my dog' backup=yes |
is there any way to achieve someting like sed 's/horse/dog/g'
?
Answer:
You can use backreferences to retrieve other parts(that should not be changed) of the line:
1 |
1 2 3 4 5 6 |
- lineinfile: dest=abc.conf state=present regexp='^(.*)horse(.*)$' line='\1dog\2' backup=yes backrefs=yes |
1 2 |
|