Ansible 文件内联不适用于循环
Ansible fileinline not working with loop
我正在尝试使用 lineinfile
在文件中添加或编辑多行,但无法正常工作。我使用下面的代码没有运气参考: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
任务 [换行] *************************************** *******************
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
在任务中的缩进不正确。
with_items
应该在模块级别,而不是作为模块本身的参数。在您的情况下,您将 with_items
作为参数传递给 lineinfile
模块,而 ansible 抱怨 lineinfile
模块没有 with_items
参数。
你的任务应该是这样的 -
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' }
我正在尝试使用 lineinfile
在文件中添加或编辑多行,但无法正常工作。我使用下面的代码没有运气参考: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
任务 [换行] *************************************** *******************
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
在任务中的缩进不正确。
with_items
应该在模块级别,而不是作为模块本身的参数。在您的情况下,您将 with_items
作为参数传递给 lineinfile
模块,而 ansible 抱怨 lineinfile
模块没有 with_items
参数。
你的任务应该是这样的 -
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' }