本文介绍了如何确保线存在并且没有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

Ansible播放:

The ansible play:

- 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:是,如果您要取消注释的行尚未存在并已注释,则游戏不会有任何改变。

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注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:43