我希望剧本与列表中的每个项目一起运行一次,而不是一次运行列表中的所有项目。
Ansible版本:2.6.1
Tasks.yaml
:
---
- name: Task 1
debug:
msg: "Message 1: {{ item }}"
with_items: "{{ messages }}"
- name: Task 2
debug:
msg: "Message 2: {{ item }}"
with_items: "{{ messages }}"
剧本:
- hosts: localhost
gather_facts: no
tasks:
- import_tasks: Tasks.yml
vars:
messages:
- 1
- 2
这是我的预期结果:
Task 1 with Item 1
Task 2 with Item 1
Task 3 with Item 1
Task 1 with Item 2
Task 2 with Item 2
Task 3 with Item 2
Task 1 with Item 3
Task 2 with Item 3
Task 3 with Item 3
但是当我执行剧本时,就像这样:
Task 1 with Item 1
Task 1 with Item 2
Task 2 with Item 1
Task 2 with Item 2
Task 3 with Item 1
Task 3 with Item 2
...
我尝试了导入和包含-两者的结果相同。
答案 0 :(得分:1)
您的playbook.yml
应该实现一个循环(注意,您无法与import_tasks
进行循环;这会引发错误):
- hosts: localhost
connection: local
gather_facts: no
vars:
messages:
- 1
- 2
- 3
tasks:
- include_tasks: Tasks.yml
loop: "{{ messages }}"
Tasks.yml
应该看起来像这样(内部没有循环):
---
- name: Task 1
debug:
msg: "Message 1: {{ item }}"
- name: Task 2
debug:
msg: "Message 2: {{ item }}"