I have a large playbook, which creates groups of VMs in a cloud and each VM needs some number of data volumes attached. I attempted to make it as modular as possible and started using roles and include_roles in loops. To do this, I have a role which loops over an include_role for the VMs and then that VM role loops over an include_role for the Volumes. The problem is that the variable for the VM name doesn’t get passed properly through the second loop to be used in the volume creation role. Should it?
basically it looks something like this:
role1
loop1: var1
include: role2
include: role3
loop2: var2
include: role4
The first pass through loop1 everything is fine. The second (and subsequent) passes when you print var1 in role4 you still get var1 set to what it was for the first pass. Here is all the code from a boiled down example (with output):
test.playbook
- hosts: localhost
connection: local
gather_facts: false
vars:
variable1: default
the_items: - name: item1
roles: - role_1
roles/role_1/tasks/main.yml
- name: “Role_1_inc_2”
include_role:
name: role_2
vars:
variable1: “{{ item }}”
with_sequence: count=2 format=“Outer%02x”
roles/role_2/task/main.yml
- name: “role_2_inc_3”
include_role:
name: role_3
roles/role_3/tasks/main.yml
- name: “role_3_inc_4”
include_role:
name: role_4
vars:
r4_name: “{{ a_item.name }}”
with_items: “{{ the_items }}”
loop_control:
loop_var: a_item
roles/role_4/tasks/main.yml
- debug:
msg: “role_4: {{ variable1 }} - {{ r4_name }}”
output:
PLAY [localhost] ***********************************************************************************************************************************************************************
TASK [role_1 : Role_1_inc_2] ***********************************************************************************************************************************************************
TASK [role_3 : role_3_inc_4] ***********************************************************************************************************************************************************
TASK [role_4 : debug] ******************************************************************************************************************************************************************
ok: [localhost] => {
“changed”: false,
“msg”: “role_4: Outer01 - item1”
}
TASK [role_3 : role_3_inc_4] ***********************************************************************************************************************************************************
TASK [role_4 : debug] ******************************************************************************************************************************************************************
ok: [localhost] => {
“changed”: false,
“msg”: “role_4: Outer01 - item1”
}
Notice the Outer loop variable is stuck at 01 for both loops.
ansible-playbook --version
ansible-playbook 2.3.0.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.13 (default, Dec 18 2016, 07:03:39) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]