添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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?

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"

and subtasks.yml:

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

There is a request to make with_items applicable to block, but it is still not implemented.

Re. making with_items applicable to block: This feature is not implemented in Ansible as explained in the feature request above. That explanation dates up to 2018-09-27, so Ansible up to v2.5.5 doesn't have this with_items+block feature; it doesn't look like it'll be added any time soon if ever. – Andrew Richards Apr 5 '19 at 14:51

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.1.3.35708