Score:1

Ansible condition to filter within loop

cn flag

I am trying to create an Ansible task to remove RDM disks from a VM when the scsi_controller value is not zero. However, the when condition is skipping the entire task rather than walking the loop and skipping only the scsi_controller(s) with value of zero. I've also tried with_nested, which produced worse results.

rdm_info (variables):

"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",
"vm_name": "test_vm"
    

Playbook task:

- name: Remove rdm disks
  community.vmware.vmware_guest_disk:
    validate_certs: false
    hostname: '{{ vc_server }}'
    username: '{{ vc_user }}'
    password: '{{ vc_pass }}'
    datacenter: '{{ datacenter_name }}'
    name: '{{ item.0 }}'
    disk:
      - state: absent
        scsi_controller: "{{ item.1 | int }}"
        unit_number: "{{ item.2 | int }}"
        destroy: no     
  loop: "{{ rdm_info | json_query('[*].vm_name') | zip( rdm_info | json_query('[*].scsi_controller') | map('split',','), rdm_info | json_query('[*].unit_number') | map('split',',')) }}"         
  when: item.1 | int != 0  
  delegate_to: localhost
  register: rdms_unmounted

I would appreciate any help, as I have already invested over 20 hours on this play.

Score:1
br flag

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
cn flag
Thank you so much Vladimir Botka, you are truly an Ansible genius.
br flag
Create a simple custom filter if you want to select multiple attributes. I added an example.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.