2 attributes
Add an attribute selection to the items, e.g.
- set_fact:
rdm_sel: "{{ rdm_sel|d([]) + [item|combine({'selection': selection})] }}"
loop: "{{ rdm_info }}"
vars:
scsi_controller: "{{ item.scsi_controller.split(',') }}"
unit_number: "{{ item.unit_number.split(',') }}"
selection: "{{ scsi_controller|
zip(unit_number)|
rejectattr('0', 'eq', '0') }}"
gives
rdm_sel:
- scsi_controller: 0,0,1,1,1,2,2,3,3,3,3
selection:
- ['1', '2']
- ['1', '0']
- ['1', '1']
- ['2', '0']
- ['2', '1']
- ['3', '0']
- ['3', '1']
- ['3', '14']
- ['3', '15']
unit_number: 0,1,2,0,1,0,1,0,1,14,15
vm_name: test_vm
Then, iterate with_subelements
- debug:
msg: >-
name: {{ item.0.vm_name }}
scsi_controller: {{ item.1.0 }}
unit_number: {{ item.1.1 }}
with_subelements:
- "{{ rdm_sel }}"
- selection
gives
msg: 'name: test_vm scsi_controller: 1 unit_number: 2'
msg: 'name: test_vm scsi_controller: 1 unit_number: 0'
msg: 'name: test_vm scsi_controller: 1 unit_number: 1'
msg: 'name: test_vm scsi_controller: 2 unit_number: 0'
msg: 'name: test_vm scsi_controller: 2 unit_number: 1'
msg: 'name: test_vm scsi_controller: 3 unit_number: 0'
msg: 'name: test_vm scsi_controller: 3 unit_number: 1'
msg: 'name: test_vm scsi_controller: 3 unit_number: 14'
msg: 'name: test_vm scsi_controller: 3 unit_number: 15'
Optionally, add all units
- set_fact:
rdm_units: "{{ rdm_units|d([]) + [item|combine({'units': units})] }}"
loop: "{{ rdm_info }}"
loop_control:
label: "{{ item.vm_name }}"
vars:
scsi_controller: "{{ item.scsi_controller.split(',') }}"
unit_number: "{{ item.unit_number.split(',') }}"
units: "{{ scsi_controller|zip(unit_number) }}"
gives
rdm_units:
- scsi_controller: 0,0,1,1,1,2,2,3,3,3,3
unit_number: 0,1,2,0,1,0,1,0,1,14,15
units:
- ['0', '0']
- ['0', '1']
- ['1', '2']
- ['1', '0']
- ['1', '1']
- ['2', '0']
- ['2', '1']
- ['3', '0']
- ['3', '1']
- ['3', '14']
- ['3', '15']
vm_name: test_vm
Then, select the units in the loop. The task below gives the same result.
- debug:
msg: >-
name: {{ item.0.vm_name }}
scsi_controller: {{ item.1.0 }}
unit_number: {{ item.1.1 }}
with_subelements:
- "{{ rdm_units }}"
- units
when: item.1.0 != '0'
Multiple attributes
Ansible doesn't provide you with a filter to zip a list of lists. But, you can create a very simple custom filter, e.g
shell> cat filter_plugins/zip2.py
def zip2(l):
return zip(*l)
class FilterModule(object):
def filters(self):
return {
'zip2': zip2,
}
Now, create a list of the attributes (_keys), extract the strings, split the items, and join the arguments for the filter zip2. Then create selection from all _keys. For example, given the simplified data for testing
rdm_info:
- x: 0,2,3
y: 4,5,6
z: 7,8,9
n: A
- x: 1,0,3
y: 4,5,6
z: 7,8,9
n: B
- x: 1,2,0
y: 4,5,6
z: 7,8,9
n: C
the task below
- set_fact:
rdm_sel: "{{ rdm_sel|d([]) + [item|combine({'selection': selection})] }}"
loop: "{{ rdm_info }}"
vars:
_keys: [x, y, z]
_args: "{{ _keys|map('extract', item)|map('split', ',')|join(',') }}"
selection: "{{ _args|zip2|rejectattr('0', 'eq', '0') }}"
gives
rdm_sel:
- n: A
selection:
- ['2', '5', '8']
- ['3', '6', '9']
x: 0,2,3
y: 4,5,6
z: 7,8,9
- n: B
selection:
- ['1', '4', '7']
- ['3', '6', '9']
x: 1,0,3
y: 4,5,6
z: 7,8,9
- n: C
selection:
- ['1', '4', '7']
- ['2', '5', '8']
x: 1,2,0
y: 4,5,6
z: 7,8,9