在多个任务上应用with_items

问题描述 投票:12回答:2

是否可以将项目列表应用于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"

是否有一些语法可以避免重复项目列表?

ansible ansible-playbook
2个回答
17
投票

截至今天你可以使用with_itemsinclude,所以你需要将你的剧本分成两个文件:

- 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,但它仍然没有实施。


5
投票

您可以在变量文件中定义yaml列表:

---
myfiles:
- "file1.sh"
- "file2.sh"
...

然后你可以使用

with_items: "{{ myfiles }}"

在任务中。

© www.soinside.com 2019 - 2024. All rights reserved.