你如何确保一行存在并且被 Ansible LineinFile 取消注释? [英] How do you Ensure a Line Exists and is Uncommented with Ansible LineinFile?

查看:27
本文介绍了你如何确保一行存在并且被 Ansible LineinFile 取消注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确保文件中存在特定行并且没有用 ansible 的 `lineinfile' 注释

How do you ensure a specific line exists in a file and is uncommented with ansible's `lineinfile'

我想要取消注释的行(在 .htaccess 中):

The line I want uncommented (in .htaccess):

#php_flag display_errors on

我使用了以下内容:

  - name: Make sure PHP Errors are turned on
    lineinfile: dest={{ www_path }}/.htaccess line="php_flag display_errors on"

推荐答案

实际上你的例子是这样的:

Actually your example works as is:

.htaccess 文件的内容:

#php_flag display_errors on

可靠的游戏:

- name: Make sure PHP Errors are turned on
  lineinfile: 
    dest: "{{ www_path }}/.htaccess"
    line: "php_flag display_errors on"

ansible-playbook 与此剧的结果:

Results of ansible-playbook with this play:

$ cat .htaccess
#php_flag display_errors on
php_flag display_errors on

如果文件以注释行开头,您将看到第二行未注释.要更正此问题,请使用将匹配现有行并替换它的正则表达式:

If the file starts with the line commented you'll see a second line uncommented. To correct this, use a regexp that will match the existing line and replace it:

- lineinfile:
    dest: /Users/bwhaley/tmp/file
    regexp: '^#php_flag display_errors'
    line: 'php_flag display_errors'
    backrefs: yes

请注意,使用 backrefs: yes 如果您想要取消注释的行尚未存在并已注释,则该播放将根本没有任何更改.

Note though that with backrefs: yes if the line you want uncommented is not already present and commented, the play will make no change at all.

这篇关于你如何确保一行存在并且被 Ansible LineinFile 取消注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆