是否可以将项目列表应用于Ansible playbook中的多个任务?举个例子:
- name: download and execute
hosts: server1
tasks:
- get_url: url="some-url/{{item}}" dest="/tmp/{{item}}"
with_items:
- "file1.sh"
- "file2.sh"
- shell: /tmp/{{item}} >> somelog.txt
with_items:
- "file1.sh"
- "file2.sh"
是否有一些语法可以避免重复项目列表?
截至今天你可以使用with_items
和include
,所以你需要将你的剧本分成两个文件:
- name: download and execute
hosts: server1
tasks:
- include: subtasks.yml file={{item}}
with_items:
- "file1.sh"
- "file2.sh"
和subtasks.yml
:
- get_url: url="some-url/{{file}}" dest="/tmp/{{file}}"
- shell: /tmp/{{file}} >> somelog.txt
有一个request使with_items
适用于block
,但它仍然没有实施。
您可以在变量文件中定义yaml列表:
---
myfiles:
- "file1.sh"
- "file2.sh"
...
然后你可以使用
with_items: "{{ myfiles }}"
在任务中。