代码之家  ›  专栏  ›  技术社区  ›  Sreeraju V

Ansible滤波中的空间问题

  •  0
  • Sreeraju V  · 技术社区  · 6 年前
    <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>
    

    2) 添加行“Include/etc/modsecurity/rules/ .conf“行后”包括可选/etc/modsecurity/ .conf文件中

    我用的是

    - 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>
    
    0 回复  |  直到 6 年前
        1
  •  3
  •   Shubham Vaishnav    6 年前

    您的任务需要一些更新,主要是在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'
    

    推荐文章