我正在尝试使用lineinfile
在文件中添加或编辑多行但不起作用。我使用下面的代码没有运气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
任务[换线] ***************************************** *****************
致命:[localhost]:失败! => {"失败":是的," msg":"字段' args'具有无效值,似乎包含未定义的变量。错误是:' item'未定义\ n \ n错误似乎出现在' /etc/ansible/playbook/test-2.yml' ;:第3行第5列,但可能在文件的其他位置,具体取决于具体情况语法问题。\ n \ n违规行似乎是:\ n \ n任务:\ n - 名称:行更改\ n ^此处\ n"}
答案 0 :(得分:3)
您的with_items
未在任务中正确缩进。
with_items
应该在模块的级别,而不是模块本身的参数。在您的情况下,您将lineinfile
作为参数传递给with_items
模块,并且ansible抱怨lineinfile
模块没有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' }
参数。
你的任务应该是这样的 -
{{1}}