D
D
Daniil Muidinov2015-12-05 12:14:35
linux
Daniil Muidinov, 2015-12-05 12:14:35

How can I use Ansible's lineinfile module to replace the first match after a given line?

Hello.
Can you tell me if it is possible, using the Ansible lineinfile module, to replace the first match after a given line and how?
There is such a file

[abc]
enabled=0
[abc-1]
enabled=0
[abc-2]
enabled=0
[abc-test]
enabled=0
[abc-debuginfo]
enabled=0

You need to replace Enabled=0 with Enabled=1 only after [abc] and [abc-2],
tried this
- lineinfile: dest=/path/to/file regexp="^Enabled=" insertafter="^\[abc\]" line="Enabled=1"
- lineinfile: dest=/path/to/file regexp="^Enabled=" insertafter="^\[abc-2\]" line="Enabled=1"

but the replacement is only the last Enabled, as I understand it, that insertafter / insertbefore is processed if regexp did not find a match.
UPD So far I solved the problem like this
- command: "sed -i '/^\[abc\]/,/\[.*\]/ { s/^enabled=.*/enabled=1/g }; /^\[abc-2\]]/,/\[.*\]/ { s/^enabled=.*/enabled=1/g }'  /path/to/file"

Perhaps there is a more correct / beautiful solution, I have not found it yet.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Puma Thailand, 2015-12-05
@opium

apparently you are trying to enable the repository, there are standard commands for this, and not some kind of inline file

Y
Yuri, 2015-12-05
@xtreme

One task - one module.
If you write two modules in one task, only the last one is executed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question