简体   繁体   English

ansible lineinfile 正则表达式多行

[英]ansible lineinfile regex multiline

I'm trying to edit apache.conf using Ansible.我正在尝试使用 Ansible 编辑 apache.conf。 Here's part of my conf:这是我的 conf 的一部分:

# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

#<Directory /srv/>
#       Options Indexes FollowSymLinks
        AllowOverride All
#       Require all granted
#</Directory>

I want to change this block我想改变这个块

<Directory /var/www/>
            Options Indexes FollowSymLinks
            AllowOverride None
            Require all granted
    </Directory>

into进入

<Directory /var/www/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>

set AllowOverride from None to All.将 AllowOverride 从无设置为全部。 I'm using this ansible task我正在使用这个 ansible 任务

- name: change htaccess support
  lineinfile:
    dest: /etc/apache2/apache2.conf
    regexp: '\s<Directory /var/www/>\n\sOptions Indexes FollowSymLinks\n\sAllowOverride'
    line: "AllowOverride All"
  tags:
    - test

However, AllowOverride All always added to the end of file.但是, AllowOverride All 总是添加到文件末尾。 What's the correct regex pattern to do this jobs.完成这项工作的正确正则表达式模式是什么。 I don't use ansible template cuz I only change one line.我不使用 ansible 模板,因为我只更改一行。

I managed to do exactly this by slightly improving @mattyoung-redhatmatt's answer.我通过稍微改进@mattyoung-redhatmatt 的回答来做到这一点。 As it turns out, the replace module handles multi line regular expressions and backreferences just fine:事实证明, replace模块可以很好地处理多行正则表达式和反向引用:

- name: AllowOverride all
  replace:
    dest=/etc/apache2/apache2.conf
    regexp='(<[dD]irectory /var/www/>[^<]*)AllowOverride None'
    replace='\1AllowOverride All'
    backup=yes
  sudo: yes
  notify:
    - restart apache

I tried and I tried to use blockinfile for this and I did not want to use a full template.我尝试过并尝试为此使用 blockinfile ,但我不想使用完整的模板。 I ended up using ansible's replace module and practicing a little regex on regex.com:我最终使用了 ansible 的替换模块并在 regex.com 上练习了一些正则表达式:

- hosts: all
  tasks:
    - name: Update apache2.conf
      replace: dest=/etc/apache2/apache2.conf
        regexp='<Directory /var/www/>\n\tOptions Indexes FollowSymLinks\n\tAllowOverride None'
        replace='<Directory /var/www/>\n\tOptions Indexes FollowSymLinks\n\tAllowOverride All'
        backup=yes
      notify:
        - restart apache2
  handlers:
    - name: restart apache2
      service: name=apache2 state=restarted

Ansible's lineinfile module does precisely just that, it deals with single lines in files and has no support for multiple lines . Ansible 的lineinfile模块正是这样做的,它处理文件中的单行并且不支持多行

This leaves you with a couple of options to tackle this problem instead.这让您有几个选择来解决这个问题。

As with most tricky things around lineinfile you might be best replacing this with a template instead.与 lineinfile 中最棘手的事情一样,您最好用模板代替它。

Alternatively you could try using the blockinfile role to first remove ( state=absent ) the following block:或者,您可以尝试使用blockinfile角色首先删除( state=absent )以下块:

        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted

after <Directory /var/www/> and then inserting ( state=present ) the following block:<Directory /var/www/>之后插入( state=present )以下块:

        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted

after <Directory /var/www/> .<Directory /var/www/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM