Ansible Fileinline无法与循环一起使用 [英] Ansible fileinline not working with loop
问题描述
我正在尝试使用lineinfile
在文件中添加或编辑多行,但无法正常工作.我正在使用以下没有运气的代码Ref: ansible:lineinfile中有几行?
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-playbook test-2.yml
任务[换行] ********************************************* *****************
TASK [change of line] **********************************************************
致命:[localhost]:失败! => {"failed":true,"msg":字段'args'的值无效,该值似乎包含未定义的变量.错误为:'item'未定义\ n \ n错误显示为已位于"/etc/ansible/playbook/test-2.yml"中:第3行第5列,但是\ n根据确切的语法问题可能\ 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抱怨说对于lineinfile
模块没有参数作为with_items
.
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.
您的任务应如下所示-
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屋!