nutanix / nutanix.ansible

Official Nutanix Ansible collections
GNU General Public License v3.0
64 stars 36 forks source link

[Bug] filtering on name results in error in module ntnx_images_info #388

Open jhowe-uw opened 1 year ago

jhowe-uw commented 1 year ago

Describe the bug

Following the examples provided for ntnx_images_info, I encounter runtime issues when applying a filter to the images_info query

We use cloud-init isos to customize the VM on deploys

We have a playbook that leaves ${hostname}-cloud-init isos on VM deploy failure.

It appears we can upload multiple instances of the same ISO with the same name. This causes issues on new VM deploys using nutanix.ncp.ntnx_vms ( 500 Internal Server Error )

To address this issue, I am attempting to sanity check for existing cloud-init isos and make a list to purge if duplicates are found.

To Reproduce Steps to reproduce the behavior:

Create 12 images named "some-host-cloud-init.iso" using the same ISO file.

Relevant vars

  module_defaults:
    group/nutanix.ncp.ntnx:
      nutanix_username: '{{ vault_nutanix_svc_account }}'
      nutanix_password: '{{ vault_nutanix_svc_password }}'
      validate_certs:   false

Playbook

- name: Create VM If It Does Not Exist
  when: does_nutanix_vm_exist.response.metadata.total_matches == 0
  block:
    - name: Sanity Check for Existing Cloud Init ISOs
      delegate_to:  localhost
      register:     existing_cloud_init_isos
      nutanix.ncp.ntnx_images_info:
        nutanix_host:   '{{ nutanix_hostname }}'
        filter:
          - name:       '{{ inventory_hostname_short }}-cloud-init.iso'
        length:         -1
        offset:         0
        sort_order:     'ASCENDING'
        sort_attribute: 'name'

Stack trace

TASK [Sanity Check for Existing Cloud Init ISOs] *********************************************************
Friday 18 August 2023  18:41:53 -0700 (0:00:01.208)       0:00:02.107 ********* 
Friday 18 August 2023  18:41:53 -0700 (0:00:01.208)       0:00:02.106 ********* 
fatal: [${FQDN} -> localhost]: FAILED! => {"changed": false, "msg": "argument 'filter' is of type <class 'list'> and we were unable to convert to dict: <class 'list'> cannot be converted to a dict"}

Expected behavior

Filtering on '{{ inventory_hostname_short }}-cloud-init.iso' should result in a json list containing the 12 instances of this file that I can parse to gather a list of UUIDs to queue for deletion.

Additional context

I tested using a known single instance of an image name and got the same error ( 'CENTOS_78' )

Please let me know if you need additional information.

Thanks

Gevorg-Khachatryan-97 commented 1 year ago

Dear @jhowe-uw, Unfortunately, there is a typo in the example, and as you can see, the error message says that the filter must be a dictionary. So it should look like this.

      nutanix.ncp.ntnx_images_info:
        nutanix_host:   '{{ nutanix_hostname }}'
        filter:
          name:       '{{ inventory_hostname_short }}-cloud-init.iso'
jhowe-uw commented 1 year ago

@Gevorg-Khachatryan-97

I tested with the new syntax and it works for me.

Thanks!