【问题标题】:How do you Ensure a Line Exists and is Uncommented with Ansible LineinFile?Ansible LineinFile 如何确保行存在且未注释?
【发布时间】:2015-10-17 07:40:18
【问题描述】:

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

我想要取消注释的行(.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"

【问题讨论】:

  • 为什么不能对整个.htaccess文件使用配置管理?

标签: .htaccess ansible ansible-playbook


【解决方案1】:

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

.htaccess 文件的内容:

#php_flag display_errors on

Ansible 游戏:

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

ansible-playbook 的结果:

$ cat .htaccess
#php_flag display_errors on
php_flag display_errors on

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

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

请注意,尽管使用backrefs: yes,如果您想要取消注释的行尚未出现并已被注释,则该剧将不会发生任何变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2023-03-10
    相关资源
    最近更新 更多