ansible lineinfile正则表达式多行

编程入门 行业动态 更新时间:2024-10-21 12:41:39
本文介绍了ansible lineinfile正则表达式多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Ansible编辑apache.conf.这是我的conf的一部分:

I'm trying to edit apache.conf using Ansible. Here's part of my 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"从无"设置为全部".我正在使用这个烦人的任务

set AllowOverride from None to All. I'm using this ansible task

- 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全部始终添加到文件末尾.什么是正确的正则表达式模式来执行此工作.我不使用ansible模板,因为我只更改一行.

However, AllowOverride All always added to the end of file. What's the correct regex pattern to do this jobs. I don't use ansible template cuz I only change one line.

推荐答案

我设法通过稍微改善@ mattyoung-redhatmatt的答案来做到这一点.事实证明,replace模块可以处理多行正则表达式和反向引用:

I managed to do exactly this by slightly improving @mattyoung-redhatmatt's answer. As it turns out, the replace module handles multi line regular expressions and backreferences just fine:

- 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

更多推荐

ansible lineinfile正则表达式多行

本文发布于:2023-11-01 12:42:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式   ansible   lineinfile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!