【问题标题】:Ansible lineinfile skipping lineAnsible lineinfile 跳线
【发布时间】:2015-09-11 22:32:39
【问题描述】:

在我的 Ansible 脚本中,我使用循环向来宾操作系统上的 apache2.conf 文件添加行。 lineinfile 模块插入除最后一行</Directory> 之外的每一行。为什么 Ansible 会跳过最后一行?谢谢。

- name: test
  lineinfile:
    dest: /etc/apache2/apache2.conf
    line: "{{ item.line }}"
  with_items: 
    - { line: <Directory /vagrant> }
    - { line: Options Indexes FollowSymLinks }
    - { line: AllowOverride All }
    - { line: Require all granted }
    - { line: </Directory> }
  notify:
    - restart apache2   

【问题讨论】:

    标签: apache2 vagrant ubuntu-14.04 ansible


    【解决方案1】:

    这是不可重现的。您确定文件中没有更高的&lt;/Directory&gt; 标记吗?

    这是我测试的任务:

      - name: test
        lineinfile:
          dest: foo.conf
          line: "{{ item.line }}"
        with_items: 
          - { line: <Directory /vagrant> }
          - { line: Options Indexes FollowSymLinks }
          - { line: AllowOverride All }
          - { line: Require all granted }
          - { line: </Directory> }
    

    还有foo.conf的内容。

    <Directory /vagrant>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    </Directory>
    

    顺便说一句,使用lineinfile 通常表明文件(本例中为apache2.conf)不在配置管理中。可能会发生许多可怕的事情,这会使这种情况发生。你最好把 apache2.conf 放在你的配置管理中。 (例如,使用 Ansible 部署它)。或者,您至少可以使用 conf.d 文件,以便 Ansible 可以“拥有”整个文件。

    【讨论】:

    • 强烈强调“你最好把 apache2.conf 放在你的配置管理中”,我建议为此使用模板模块。
    【解决方案2】:

    我认为这是模块 lineinfile 中的一个错误。

    &lt;/Directory&gt; 行在文件 apache2.conf 中有呈现,而 Ansible lineinfile 跳过它。

    我已经向 Ansible 团队报告了this bug

    【讨论】:

      【解决方案3】:

      注意从 Ansible 2.0(2016 年 1 月发布,在提出这个问题之后)开始,最好使用 blockinfile 实现:

      - name: insert vagrant directory
        blockinfile:
          path: /etc/apache2/apache2.conf
          block: |
            <Directory /vagrant>
              Options Indexes FollowSymLinks
              AllowOverride All
              Require all granted
            </Directory>
        Match User ansible-agent
        PasswordAuthentication no 
      

      参考:http://docs.ansible.com/ansible/latest/blockinfile_module.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-30
        • 1970-01-01
        • 2019-06-03
        • 1970-01-01
        • 2023-03-10
        相关资源
        最近更新 更多