簡體   English   中英

lineinfile支持多行匹配嗎?

[英]Are multiline matches supported in lineinfile?

我想匹配(並用一條注釋行刪除或替換)一行代碼塊:

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

我試圖在lineinfile它們與daemon(?:.|\\n)*xconsole匹配,但是似乎沒有發生匹配:添加了替換行,但舊行仍然存在:

  - 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"

是否支持此類塊?

注意:我知道關於blockinfile ,它對於管理定界塊的添加/刪除非常blockinfile 我不認為它們可以與不可插入的塊(通過正則表達式匹配)一起使用。

否, lineinfile搜索表達式,請參見模塊的源代碼

如果需要刪除/替換文本,請使用replace模塊-它使用多行正則表達式,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM