regex - ansible lineinfile 正则表达式 多行

标签 regex apache ansible

我正在尝试使用 Ansible 编辑 apache.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>

我想改变这个 block
<Directory /var/www/>
            Options Indexes FollowSymLinks
            AllowOverride None
            Require all granted
    </Directory>

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

将 AllowOverride 从 None 设置为 All。我正在使用这个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

但是,AllowOverride All 始终添加到文件末尾。做这项工作的正确正则表达式模式是什么。我不使用 ansible 模板,因为我只更改了一行。

最佳答案

通过稍微改进@mattyoung-redhatmatt 的答案,我设法做到了这一点。事实证明,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

关于regex - ansible lineinfile 正则表达式 多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33094075/

相关文章:

javascript - 如何获得两个词之间的所有内容

股票期权不匹配的 Python 正则表达式

java - 正则表达式不匹配

node.js - 转发端口从 80 和 443 到 8080

pip - `ansible --version` 命令抛出错误

ubuntu - 如何使用ansible在目标上提取war文件

ansible - 如何访问用于调用 Ansible 的命令行参数?

c# - 使用正则表达式匹配但不包含在结果中

http - 无法分配请求的地址 : make_sock: could not bind to address

apache - 将 Apache 配置为多个域的反向代理