dulhaver
(Gunnar)
October 11, 2023, 11:06am
1
I am playing with lineinfile
target file before
1 line 1
2 line 2
3 line 3
4 line 4
5 line 5
6 byebye
My playboook
---
- name: insert multi-line something
hosts: "{{ targets | default('postgres') }}"
tasks:
- name: Insert multi-line content into a file
lineinfile:
path: /home/postgres/somefile_231011
regexp: '^line 3'
line: |
this is my
multi line
content
become: true
become_user: postgres
target after 1st run
(as expected)
1 line 1
2 line 2
3 this is my
4 multi line
5 content
6 line 4
7 line 5
8 byebye
target file after second run
(this should be identical from the above, but adds my 3 lines plus an additional empty line at the bottom)
1 line 1
2 line 2
3 this is my
4 multi line
5 content
6 line 4
7 line 5
8 byebye
9 this is my
10 multi line
11 content
12
what am I missing?
I though the regexp
would make this idempotent (but apparently doesn’t work)
Do I need additionally insertberfore
/insertafter
?
jpmens
(Jan-Piet Mens)
October 11, 2023, 1:09pm
2
I think this is correct behaviour. The doc states about regexp
:
If the regular expression is not matched, the line will be added to the file in keeping with insertbefore
or insertafter
settings.
Might you want the blockinfile
module?
2 Likes
mophahr
(Moritz)
October 11, 2023, 1:09pm
3
Blockinfile seems to be what you’re looking for.
3 Likes
dulhaver
(Gunnar)
October 11, 2023, 2:00pm
4
maybe, yes. Would you say the blobk I am rying to sneak in lineinfile
with …
line: |
here are
my lines
of code
… part of the problem, or is the takeaway that lineinfile
is simpy not idempotent really ?
jpmens
(Jan-Piet Mens)
October 12, 2023, 7:18am
5
I think lineinfile
is idempotent, but as your first run has removed the line you’re using as anchor in the regex, it has no other alternative than to append to the end of the file (or the beginning) depending on the insert*
parameter.
dulhaver:
regexp: '^line 3'
1 Like
the problem is with the regex,. In the second excution the line that matcht with the regex does not exist because it was replace in the first execution, so linefile does not found any match for that regex and it ends adding it to the end of the file
1 Like
system
(system)
Closed
November 23, 2023, 9:29pm
7
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.