I need to somehow loop over a list of variables and execute both of the below roles once for each iteration, on each iteration passing a variable to the role. For example, given a variable list of 100-101, I need to execute in the order role1:100, role2:100, role1:101, role2:101. The variables 100-100 should be passed to the tasks inside the role.
---
- hosts: group1
tasks:
- include_role:
name: role1
- hosts: group2
tasks:
- include_role:
name: role2
I was looking at the below answer as a possible solution but I am not sure how to adapt it to my needs. Can the above scenario be accomplished in Ansible?
Loop over the Cartesian product of variables and role names:
vars:
roles_to_include:
- role1
- role2
values_to_pass:
- 100
- 101
tasks:
- include_role:
name: "{{ item.1 }}"
vars:
my_variable: "{{ item.0 }}"
loop: "{{ values_to_pass | product(roles_to_include) | list }}"
来源:https://stackoverflow.com/questions/51616416/how-to-execute-alternating-roles-in-ansible