将 with_items 应用于多个任务 [英] Apply with_items on multiple tasks
本文介绍了将 with_items 应用于多个任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将项目列表应用于 Ansible 剧本中的多个任务?举个例子:
Is it possible to apply a list of items to multiple tasks in an Ansible playbook? To give an example:
- 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"
是否有一些语法可以避免项目列表的重复?
Is there some syntax to avoid the repetition of the item-list?
推荐答案
截至今天,您可以将 with_items
与 include
一起使用,因此您需要拆分您的剧本分成两个文件:
As of today you can use with_items
with include
, so you'd need to split your playbook into two files:
- 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
有一个请求使with_items
适用于block
,但 Ansible 团队表示永远不会支持它.
There was a request to make with_items
applicable to block
, but the Ansible team has said it will never be supported.
这篇关于将 with_items 应用于多个任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文