我正在尝试使用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>
我想更改此块
<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。我正在使用这个安全的任务
- 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模板因为我只改变了一行。
答案 0 :(得分:5)
我设法通过略微提高@ 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
答案 1 :(得分:2)
我试过,我尝试使用blockinfile,我不想使用完整的模板。我最终使用了ansible的替换模块并在regex.com上练习了一点regex:
- 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
答案 2 :(得分:1)
Ansible的lineinfile模块恰恰就是这样,它处理文件中的单行并且no support for multiple lines。
这为您提供了几个选项来解决这个问题。
与lineinfile周围最棘手的事情一样,您可能最好用template替换它。
或者,您可以尝试使用blockinfile角色首先删除(state=absent
)以下块:
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
在<Directory /var/www/>
之后,然后插入(state=present
)以下块:
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
在<Directory /var/www/>
之后。
答案 3 :(得分:0)
我遇到了类似的问题,并且可以使用“ linefile”模块,因为conf中的行是如此相似。因此,我首先要删除要编辑的块:
- name: apache2 remove orginal <Directory /var/www/> text block
shell: sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ d' /etc/apache2/apache2.conf warn=False
然后引入ansible受管区块
- name: apache2 Insert Ansible managed text block for <Directory /var/www/> in order to remove Apache Default landingpage
blockinfile:
path: /etc/apache2/apache2.conf
marker: "# {mark} ANSIBLE MANAGED BLOCK"
marker_begin: var_www_begin
marker_end: var_www_end
block: |
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>