WoJ
WoJ

Reputation: 30035

Are multiline matches supported in lineinfile?

I would like to match (and remove or replace with one commented line) a block of lines:

daemon.*;mail.*;\
        news.err;\
        *.=debug;*.=info;\
        *.=notice;*.=warn       |/dev/xconsole

I tried to match them in lineinfile with daemon(?:.|\n)*xconsole but the match does not seem to happen: a replacement line is added but the old line remains:

  - name: remove xconsole from rsyslog.conf
    lineinfile:
      dest: /etc/rsyslog.conf
      regexp: daemon(?:.|\n)*xconsole
      state: absent
      # also tried to add the next line to replace with a comment
      #line: "# removed by ansible"

Are such blocks supported?

Note: I know about blockinfile which is great to manage the addition/removal of delimited blocks. I do not believe that they work with non-ansible-inserted blocks (matched though a regex).

Upvotes: 1

Views: 3113

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68299

No, lineinfile search expression line by line, see module's source code.

If you need to remove/replace text, use replace module – it use multiline regex, e.g.:

 - name: remove xconsole from rsyslog.conf
    replace:
      dest: /etc/rsyslog.conf
      # ensure regex is lazy!
      regexp: daemon[\S\s]*?xconsole
      replace: "# removed by ansible"

Upvotes: 2

Related Questions