gpt4 book ai didi

regex - ansible lineinfile 正则表达式 多行

转载 作者:行者123 更新时间:2023-12-04 11:36:06 27 4
gpt4 key购买 nike

我正在尝试使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com