Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One loop over multiple Ansible tasks

I've created an Ansible playbook that creates a cloud instance and then installs some programs on the instance. I want to run this playbook multiple times (without using a bash script). Is it possible to use a loop to loop over those two tasks together (I.E. One loop for two tasks?). All I've been able to find so far is one loop for each individual task

like image 266
AverageWorker Avatar asked Jun 11 '15 15:06

AverageWorker


1 Answers

An update:

In 2.0 you are able to use with_ loops and task includes (but not playbook includes), this adds the ability to loop over the set of tasks in one shot. There are a couple of things that you need to keep in mind, a included task that has it’s own with_ loop will overwrite the value of the special item variable. So if you want access to both the include’s item and the current task’s item you should use set_fact to create a alias to the outer one.:

- include_tasks: test.yml 
  with_items:     - 1     - 2     - 3 

in test.yml:

- set_fact: outer_loop="{{item}}" - debug: msg="outer item={{outer_loop}} inner item={{item}}" 
  with_items:     - a     - b     - c 

Source: Ansible Docs

like image 198
Wtower Avatar answered Sep 20 '22 11:09

Wtower