使用Ansible进行过滤时的空间问题

问题描述 投票:0回答:1
<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 usrsharemodsecurity-crs*.load "行。

2) 在文件中增加 "Include etcmodsecurityrules "行。.conf "行后的 "IncludeOptional etcmodsecurity.conf "文件中的

我使用的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 ansible-2.x ansible-inventory
1个回答
3
投票

你的任务需要更新一下,主要是在regex方面,请使用下面的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'

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

© www.soinside.com 2019 - 2024. All rights reserved.