I am using a nested loop in Ansible ("create 3 VMs for each of the 10 users"):
- name: Add hosts to inventory
add_host:
name: "{{ '%s-%02d-%02d' | format(vm_prefix, item.0, item.1.number) }}"
groups: vms
loop: "{{userlist | product(vms_per_user) | list }}"
My question is - do I have any way of getting the index of an item in the second list?
- name: Add hosts to inventory
add_host:
name: "{{ '%s-%02d-%02d' | format(vm_prefix, item.0, item.1.number) }}"
groups: vms
vm_index: "{{ get the index of this particular VM in vms_per_user }}"
loop: "{{userlist | product(vms_per_user) | list }}"
I know about with_indexed_items
and flatten + loop_control.index
, but I cannot figure out how to write this so that I will get an index that loops only on the second list, and restarts from 0 for every new user (every new element in the first list).
TL;DR - I am looking for the ansible equivalent of this Python construct:
for user in users:
for (index, vm_name) in enumerate(vms_per_user):
do_something_with user, index, vm_name
Thank you!