Yaml Ansible fileinline不使用循环

Yaml Ansible fileinline不使用循环,yaml,ansible,ansible-playbook,Yaml,Ansible,Ansible Playbook,我试图使用lineinfle在文件中添加或编辑多行,但不起作用。我正在使用以下代码,但没有运气参考: 可能的错误: # ansible-playbook test-2.yml 任务[换行]********************************************************** 致命:[localhost]:失败!=>{“failed”:true,“msg”:"字段“args”有一个无效值,该值似乎包含一个未定义的变量。错误是:“item”未定义\n\n错误似乎出现在

我试图使用
lineinfle
在文件中添加或编辑多行,但不起作用。我正在使用以下代码,但没有运气参考:

可能的错误:

# ansible-playbook test-2.yml
任务[换行]**********************************************************

致命:[localhost]:失败!=>{“failed”:true,“msg”:"字段“args”有一个无效值,该值似乎包含一个未定义的变量。错误是:“item”未定义\n\n错误似乎出现在“/etc/ansible/playbook/test-2.yml”:第3行第5列,但可能\n出现在文件的其他位置,具体取决于语法问题。\n\n出现问题的行是:\n\n tasks:\n-name:更改行\n^此处\n“}


您的带有项目的
未在任务中正确缩进

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

您的任务应该如下所示-

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' }

您的带有项目的
未在任务中正确缩进

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

您的任务应该如下所示-

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' }