问题描述
我正在尝试使用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] **********************************************************
推荐答案
您的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无法与循环一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!