Ansible fileinline 不适用于循环 [英] Ansible fileinline not working with loop

查看:37
本文介绍了Ansible fileinline 不适用于循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 lineinfile 在文件中添加或编辑多行,但不起作用.我使用下面的代码没有运气 Ref: ansible: lineinfile for many lines?

I am trying to add or edit multiple lines in a file using lineinfile but not working. I am using below code with no luck Ref: 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 [换行]*****************************************************************

TASK [change of line] **********************************************************

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

fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'item' is undefined\n\nThe error appears to have been in '/etc/ansible/playbook/test-2.yml': line 3, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: change of line\n ^ here\n"}

推荐答案

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

Your with_items is not indented correctly within the task.

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

with_items should be at the level of the module, not as a parameter to the module itself. In your case, you are passing with_items as a parameter to lineinfile module, and ansible is complaining that there is no parameter as with_items for lineinfile module.

你的任务应该是这样的 -

Your task should look something like this -

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 fileinline 不适用于循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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