Apply with_items to multiple tasks

Can I apply a list of items to multiple tasks in an Ansible downloadable book? 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 any syntax to avoid repeating the list of elements?

+10
source share
3 answers

Today you can use with_items with include , so you need to split your game 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.

+12
source

You have the option of defining a yaml list in a variable file:

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

and then you can use

 with_items: "{{ myfiles }}" 

in the task.

+5
source

This feature is not implemented in Ansible, as explained in this request . This explanation dates from 2018-09-27, so Ansible prior to v2.5.5 does not have this feature; it doesn't look like it will be added any time soon.

0
source

All Articles