Ansible 与多个任务上的\u项一起应用

Ansible 与多个任务上的\u项一起应用,ansible,ansible-playbook,Ansible,Ansible Playbook,是否可以在Ansible剧本中将项目列表应用于多个任务?举个例子: - 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 wit

是否可以在Ansible剧本中将项目列表应用于多个任务?举个例子:

- 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"

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

从今天起,您可以将
与项目一起使用
一起包含
,因此您需要将playbook拆分为两个文件:

- name: download and execute
  hosts: server1
  tasks:
  - include: subtasks.yml file={{item}}
    with_items:
    - "file1.sh"
    - "file2.sh"
子任务.yml

- get_url: url="some-url/{{file}}" dest="/tmp/{{file}}"
- shell: /tmp/{{file}} >> somelog.txt

有一种方法可以使
中的项目
适用于
,但Ansible团队表示永远不支持它

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

---
myfiles:
- "file1.sh"
- "file2.sh"
...
然后你可以使用

with_items: "{{ myfiles }}"

在任务中。

Re。使带有_项的
适用于
:此功能未在Ansible中实现,如上述功能请求中所述。该解释可追溯至2018年9月27日,因此Ansible至v2.5.5版不具有此
和\u项
+
功能;它看起来不会很快被添加。最新的
include
模块文档(从v2.9开始)表明
include
模块将在不久的将来被弃用,并建议改用
include\u tasks
import\u tasks
模块。