Space 使用 Ansible 过滤时出现问题

Space issue while filtering with Ansible

<IfModule security2_module>
        # Default Debian dir for modsecurity's persistent data
        SecDataDir /var/cache/modsecurity

        # Include all the *.conf files in /etc/modsecurity.
        # Keeping your local configuration in that directory
        # will allow for an easy upgrade of THIS file and
        # make your life easier
        IncludeOptional /etc/modsecurity/*.conf

        # Include OWASP ModSecurity CRS rules if installed
        IncludeOptional /usr/share/modsecurity-crs/*.load
</IfModule>

我想做的是

1) 从文件中删除“IncludeOptional /usr/share/modsecurity-crs/*.load”行。

2) 在文件

我使用的 Ansible 脚本是

- name: Removing line from file
  lineinfile:
     dest: /etc/apache2/mods-enabled/security2.conf
     regexp: 'IncludeOptional /usr/share/modsecurity-crs/*.load'
     state: absent
- name: Insert new line in the file after line
  lineinfile:
    dest: /etc/apache2/mods-enabled/security2.conf
    line: 'Include /etc/modsecurity/rules/*.conf'
    insertafter: 'IncludeOptional /etc/modsecurity/*.conf'   

但是由于该行前面的空格,我无法添加或删除任何行。我在指定正则表达式时做错了吗?

我最终想要实现的是:

<IfModule security2_module>
        # Default Debian dir for modsecurity's persistent data
        SecDataDir /var/cache/modsecurity

        # Include all the *.conf files in /etc/modsecurity.
        # Keeping your local configuration in that directory
        # will allow for an easy upgrade of THIS file and
        # make your life easier
        IncludeOptional /etc/modsecurity/*.conf
        Include /etc/modsecurity/rules/*.conf

        # Include OWASP ModSecurity CRS rules if installed
</IfModule>

您的任务需要一些更新,主要是在正则表达式中,请使用以下 ansible 任务来达到预期的结果。

- name: Removing line from file
  lineinfile:
     dest: test.sh
     regexp: '^\s*IncludeOptional /usr/share/modsecurity-crs/\*.load'
     state: absent

- name: Insert new line in the file after line
  lineinfile:
    dest: test.sh
    line: '        Include /etc/modsecurity/rules/*.conf'
    insertafter: '^\s*IncludeOptional /etc/modsecurity/\*.conf'

第一个任务从文件中删除行,第二个任务在找到模式后插入给定行。