【问题标题】:Ansible fileinline not working with loopAnsible fileinline 不适用于循环
【发布时间】:2017-01-26 15:02:50
【问题描述】:

我正在尝试使用 lineinfile 在文件中添加或编辑多行但不工作。我使用下面的代码没有运气参考:ansible: lineinfile for several lines?

# vim /etc/ansible/playbook/test-play.yml 

- hosts: tst.wizvision.com
  tasks:
  - name: change of line
    lineinfile:
      dest: /root/test.txt
      regexp: "{{ item.regexp }}"
      line: "{{ item.line }}"
      backrefs: yes
      with_items:
      - { regexp: '^# line one', line: 'NEW LINE ONE' }
      - { regexp: '^# line two', line: 'NEW LINE TWO' }

Ansible 错误:

# ansible-playbook test-2.yml

TASK [换行] ***************************************** *****************

致命:[本地主机]:失败! => {"failed": true, "msg": "字段 'args' 的值无效,似乎包含未定义的变量。错误是:'item' 未定义\n\n错误似乎是已在 '/etc/ansible/playbook/test-2.yml': 第 3 行第 5 列,但可能\n在文件中的其他位置,具体取决于确切的语法问题。\n\n违规行似乎是:\ n\n 任务:\n - 名称:换行\n ^ 此处\n"}

【问题讨论】:

    标签: yaml ansible ansible-playbook


    【解决方案1】:

    您的with_items 在任务中的缩进不正确。

    with_items 应该在模块级别,而不是作为模块本身的参数。在您的情况下,您将with_items 作为参数传递给lineinfile 模块,而ansible 抱怨lineinfile 模块没有with_items 的参数。

    你的任务应该是这样的 -

    tasks:
    - name: change of line
      lineinfile:
        dest: /root/test.txt
        regexp: "{{ item.regexp }}"
        line: "{{ item.line }}"
        backrefs: yes
      with_items:
        - { regexp: '^# line one', line: 'NEW LINE ONE' }
        - { regexp: '^# line two', line: 'NEW LINE TWO' }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-05
      • 2014-01-29
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多