如何在 Ansible 中取消注释多于一行?

How to uncomment more than one line in Ansible?

我想取消这条线,但 "privides UDP/TCP..." 留下评论:

# provides UDP syslog reception
# module(load="imudp")
# input(type="imudp" port="514")

# provides TCP syslog reception
# module(load="imudp")
# input(type="imudp" port="514")

这是我当前取消注释一行的任务:

- name: Change rsyslog configuration
  lineinfile:
    dest: /etc/rsyslog.conf
    regex: '^module(load="imudp")'
    line: 'module(load="imudp")'

但是如何扩展此任务以取消注释更多行?我认为可以在 regex 中添加变量并使用循环 with_items 解析值,但不知道如何实现.执行此操作的最佳做​​法是什么?

模块 lineinfile 会将 放入文件,即使 regex 不匹配。

下面的任务

  tasks:
    - lineinfile:
        # firstmatch: true
        dest: rsyslog.conf
        regex: '^#\s*{{ item.regex }}(.*)$'
        line: '{{ item.line }}'
      loop:
        - regex: 'module\(load="imudp"\)'
          line: 'module(load="imudp")'
        - regex: 'input\(type="imudp" port="514"\)'
          line: 'input(type="imudp" port="514")'

给予

# provides UDP syslog reception
# module(load="imudp")
# input(type="imudp" port="514")

# provides TCP syslog reception
module(load="imudp")
input(type="imudp" port="514")

和 "firstmatch: true" 给出

# provides UDP syslog reception
module(load="imudp")
input(type="imudp" port="514")

# provides TCP syslog reception
# module(load="imudp")
# input(type="imudp" port="514")

模块 replace 将替换文件中模式的所有实例

- replace:
    dest: rsyslog.conf
    regexp: '^#\s*{{ item.regex }}(.*)$'
    replace: '{{ item.replace }}'
  loop:
    - regex: 'module\(load="imudp"\)'
      replace: 'module(load="imudp")'
    - regex: 'input\(type="imudp" port="514"\)'
      replace: 'input(type="imudp" port="514")'

给予

# provides UDP syslog reception
module(load="imudp")
input(type="imudp" port="514")

# provides TCP syslog reception
module(load="imudp")
input(type="imudp" port="514")