Проблема с пространством при фильтрации с помощью Ansible - PullRequest
1 голос
/ 17 июня 2020
<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 ) Добавьте строку «Include /etc/modsecurity/rules/.conf» после строки «IncludeOptional /etc/modsecurity/.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>

1 Ответ

3 голосов
/ 17 июня 2020

Ваши задачи требуют небольшого обновления и в основном в регулярном выражении, пожалуйста, используйте следующие 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'

Первая задача удаляет строку из файла и вторая задача вставляет данную строку после того, как шаблон найден.

...