Lineinfile - replacing regex instead of placing text after insertafter

262 views
Skip to first unread message

Listing

unread,
May 28, 2017, 5:53:05 AM5/28/17
to Ansible Project
Hi guys,

I have some problems with the lineinfile module. I try to add 'inserted_text' after '#insertbelow' when 'this' has been found in /etc/ansible/lineinfile/lol. This is my task:

---
- hosts: localhost
  tasks:
  - name: lol
    lineinfile:
      path: /etc/ansible/lineinfile/lol
      regexp: '^this'
      insertafter: '^#insertbelow'
      line: 'inserted_text'


This is the content of the lol file:

adsfsadfasdf
this

asdfasdf

#insertbelow

adfasdf

adsfa




When running the playbook, this is the result:

adsfsadfasdf
inserted_text

asdfasdf

#insertbelow

adfasdf

adsfa


When running the playbook again, this is the result:

adsfsadfasdf
inserted_text

asdfasdf

#insertbelow
inserted_text

adfasdf

adsfa

(and after running it again, it keeps adding after #insertbelow).

What the heck? It just replaced the regex text instead of placing it after #insertbelow. Am I doing something wrong?

Kai Stian Olstad

unread,
May 28, 2017, 1:38:16 PM5/28/17
to ansible...@googlegroups.com
On 28. mai 2017 11:53, Listing wrote:
> Hi guys,
>
> I have some problems with the lineinfile module. I try to add
> 'inserted_text' after '#insertbelow' when 'this' has been found in
> /etc/ansible/lineinfile/lol. This is my task:

That is not how lineinfile work.
It searches for a string, and replace it or add the line.

To do this you would need to combine find module to search for "this"
and then run lineinfile on the file.

--
Kai Stian Olstad

Listing

unread,
May 29, 2017, 10:26:54 AM5/29/17
to Ansible Project, ansible-pr...@olstad.com
Hi Kai,

Thank you for your reply. But what is the difference then between regexp and insertafter etc? Is regexp always being replaced? Or what is exactly the logic?

Kai Stian Olstad

unread,
May 30, 2017, 3:02:44 AM5/30/17
to ansible...@googlegroups.com
On 29.05.2017 16:26, Listing wrote:
> Hi Kai,
>
> Thank you for your reply. But what is the difference then between
> regexp
> and insertafter etc? Is regexp always being replaced? Or what is
> exactly
> the logic?

regexp: and insertafter: is separate features in lineinfile.

When lineinfile find the regexp in regexp: it replaces this line with
the string in line:
If it can't find the regexp in regexp: the sting in line: is append to
end of file(EOF).

When lineinfile find the regexp in insertafter: it add the string in
line: after this line, if it's not already exist.
If it can't find the regexp in insertafter: the string in line: is
append to end of file(EOF).

These two feature is not meant to be combine in one task, but used in
separate tasks.

--
Kai Stian Olstad

Listing

unread,
May 30, 2017, 7:33:23 AM5/30/17
to Ansible Project, ansible-pr...@olstad.com
Very clear, thank you!

Listing

unread,
Jul 15, 2017, 11:19:30 AM7/15/17
to Ansible Project, ansible-pr...@olstad.com
Hi Kai,

When looking in ansible-doc lineinfile, it does combine both regexp and insertafter in one task:

- lineinfile:
    path: /etc/httpd/conf/httpd.conf
    regexp: '^Listen '
    insertafter: '^#Listen '
    line: 'Listen 8080

Is this an error in the docs or are we (I?) missing something?

Kai Stian Olstad

unread,
Jul 22, 2017, 9:14:53 AM7/22/17
to ansible...@googlegroups.com
On 15.07.2017 17:19, Listing wrote:
> Hi Kai,
>
> When looking in ansible-doc lineinfile, it does combine both regexp and
> insertafter in one task:
>
> - lineinfile:
> path: /etc/httpd/conf/httpd.conf
> regexp: '^Listen '
> insertafter: '^#Listen '
> line: 'Listen 8080
>
> Is this an error in the docs or are we (I?) missing something?

If you remove the regexp line it will work exactly the same.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages