Reputation: 321
I am trying to remove blank lines between two lines using Ansible but cannot get it work. My source file looks like this:
###This line has blank lines after it
###End of block
#More lines
And I would like the result to look like this:
###This line has blank lines after it
###End of block
#More lines
So far I have this task using the replace module:
- name: Remove blank lines between matches
replace:
path: /home/vagrant/abcd
after: '###This line has blank lines after it'
regexp: '(^\n)'
replace: ''
before: '###End of block'
But the task does not appear to work. In fact the task does not do anything to the source file.
If I remove the last line of the task (before: '###End of block'). The lines do get removed but it also removes all blank lines until EOF which is not what I want. Here is the result with the last line removed:
###This line has blank lines after it###End of block
#More lines
Which is not expected.
I have a solution using sed but I want to keep it idempotent as possible. Maybe I need better regex?
Any ideas please?
Thanks.
Upvotes: 4
Views: 8548
Reputation: 321
After playing with this some more and playing with the regex I've come up with this:
- name: Remove blank lines between matches
replace:
path: /home/vagrant/abcd
after: '###This line has blank lines after it'
regexp: '(^\s*$)'
replace: ''
This produces:
###This line has blank lines after it
###End of block
#More lines
Which is what I was after. But I will wait until the bug is fixed before using in production.
Upvotes: 2
Reputation: 358
According to this issue: https://github.com/ansible/ansible/issues/31354 using after and before parameters together currently does not work as expected.
You can delete after paremeter from your task and in this case it will work as expected, but I know that it is not what you want to achieve.
Upvotes: 1