Select specific cluster with prefix in VMware cluster from Ansible

Viewed 32

I'm using the below playbook to list all the VMware cluster belonging to my datacenter, but I need to select a specific cluster containing LAB in the cluster name.

- hosts: localhost
  vars_files: 1credentials.yml
  tasks:
    - name: Gather cluster info from given datacenter
      community.vmware.vmware_cluster_info:
        hostname: '{{ vcenter_hostname }}'
        username: '{{ vcenter_username }}'
        password: '{{ vcenter_password }}'
        datacenter: SDx-CoE-T7
        validate_certs: no
      delegate_to: localhost
      register: cluster_info

    - debug:
        msg: "{{ cluster_info }}"

    - debug:
        msg: "{{ cluster_info.clusters.keys() | list | to_yaml }}"

I'm trying to use selectattr(), but, I am not able to get the exact syntax. Can you please suggest the best option to filter the cluster name containing LAB.

Below is the playbook output:

PLAY [localhost] *******************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]

TASK [Gather cluster info from given datacenter] ***********************************************************************************************************************
ok: [localhost]

TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": false,
        "clusters": {
            "TEST_LAB": {
                "datacenter": "TEST_CoE-T7",
                "drs_default_vm_behavior": "fullyAutomated",
                "drs_enable_vm_behavior_overrides": true,
                "drs_vmotion_rate": 3,
                "enable_ha": false,
                "enabled_drs": false,
                "enabled_vsan": false,
                "ha_admission_control_enabled": true,
                "ha_failover_level": 1,
                "ha_host_monitoring": "enabled",
                "ha_restart_priority": [
                    "medium"
                ],
                "ha_vm_failure_interval": [
                    30
                ],
                "ha_vm_max_failure_window": [
                    -1
                ],
                "ha_vm_max_failures": [
                    3
                ],
                "ha_vm_min_up_time": [
                    120
                ],
                "ha_vm_monitoring": "vmMonitoringDisabled",
                "ha_vm_tools_monitoring": [
                    "vmMonitoringDisabled"
                ],
                "hosts": [],
                "moid": "domain-c28615",
                "resource_summary": {
                    "cpuCapacityMHz": 0,
                    "cpuUsedMHz": 0,
                    "memCapacityMB": 0,
                    "memUsedMB": 0,
                    "pMemAvailableMB": 0,
                    "pMemCapacityMB": 0,
                    "storageCapacityMB": 0,
                    "storageUsedMB": 0
                },
                "tags": [],
                "vsan_auto_claim_storage": false
            },
            "TEST_LaaS": {
                "datacenter": "TEST_CoE-T7",
                "drs_default_vm_behavior": "fullyAutomated",
                "drs_enable_vm_behavior_overrides": true,
                "drs_vmotion_rate": 3,
                "enable_ha": true,
                "enabled_drs": true,
                "enabled_vsan": false,
                "ha_admission_control_enabled": true,
                "ha_failover_level": 1,
                "ha_host_monitoring": "enabled",
                "ha_restart_priority": [
                    "medium"
                ],
                "ha_vm_failure_interval": [
                    30
                ],
                "ha_vm_max_failure_window": [
                    -1
                ],
                "ha_vm_max_failures": [
                    3
                ],
                "ha_vm_min_up_time": [
                    120
                ],
                "ha_vm_monitoring": "vmMonitoringDisabled",
                "ha_vm_tools_monitoring": [
                    "vmMonitoringDisabled"
                ],
                "hosts": [
                    {
                        "folder": "/TEST_CoE-T7/host/TEST_LaaS",
                        "name": "172.17.65.84"
                    },
                    {
                        "folder": "/TEST_CoE-T7/host/TEST_LaaS",
                        "name": "172.17.65.85"
                    },
                    {
                        "folder": "/TEST_CoE-T7/host/TEST_LaaS",
                        "name": "172.17.168.202"
                    }
                ],
                "moid": "domain-c861",
                "resource_summary": {
                    "cpuCapacityMHz": 158040,
                    "cpuUsedMHz": 17494,
                    "memCapacityMB": 786073,
                    "memUsedMB": 361592,
                    "pMemAvailableMB": 0,
                    "pMemCapacityMB": 0,
                    "storageCapacityMB": 22707456,
                    "storageUsedMB": 14326118
                },
                "tags": [],
                "vsan_auto_claim_storage": false
            }
        },
        "failed": false
    }
}

TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "[TEST_Automation, TEST_LAB, TEST_LaaS,]\n"
}

If I use ansible version 2.9.27, I'm getting below error:

fatal: [localhost]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ cluster_info.clusters | dict2items | selectattr('key','contains','Common')| items2dict | list }}): items2dict requires a list, got <class 'generator'> instead."}

I tried with below one removing items2dict:

    - debug:
        msg: "{{ cluster_info.clusters | dict2items | selectattr('key','contains','TEST') | list }}"

It is giving the below output but I need only the key: TEST_Lab

ok: [localhost] => {
    "msg": [
        {
            "key": "TEST_LAB",
            "value": {
                "drs_default_vm_behavior": "partiallyAutomated",
                "drs_enable_vm_behavior_overrides": true,
                "drs_vmotion_rate": 3,
                "enable_ha": false,
                "enabled_drs": true,
                "enabled_vsan": false,
                "ha_admission_control_enabled": true,
                "ha_failover_level": 1,
                "ha_host_monitoring": "enabled",
                "ha_restart_priority": [
                    "medium"
                ],
                "ha_vm_failure_interval": [
                    30
                ],
                "ha_vm_max_failure_window": [
                    -1
                ],
                "ha_vm_max_failures": [
                    3
                ],
                "ha_vm_min_up_time": [
                    120
                ],
                "ha_vm_monitoring": "vmMonitoringDisabled",
                "ha_vm_tools_monitoring": [
                    "vmMonitoringDisabled"
                ],
                "tags": [],
                "vsan_auto_claim_storage": false
            }
        }
    ]
}
1 Answers

In order to use selectattr, you need to have a value to filter on, not a key. But transforming a dictionary into a list, containing the key and value is as simple as using the dict2items filter, in Ansible.
Then, to come back to your dictionary, just use the "antonym" filter, items2dict.

So, your debug task ends up being:

- debug:
    var: >-
      cluster_info.clusters
      | dict2items
      | selectattr('key','contains','LAB')
      | items2dict

Given the task, with a reduced dictionary for readability:

- debug:
    var: |-
      cluster_info.clusters
      | dict2items
      | selectattr('key','contains','LAB')
      | items2dict
  vars:
    cluster_info:
      clusters:
        TEST_LAB:
          datacenter: TEST_CoE-T7
          drs_default_vm_behavior: fullyAutomated
        TEST_LaaS:
          datacenter: TEST_CoE-T7
          drs_default_vm_behavior: fullyAutomated

This will yield:

ok: [localhost] => 
  ? |-
    cluster_info.clusters
    | dict2items
    | selectattr('key','contains','LAB')
    | items2dict
  : TEST_LAB:
      datacenter: TEST_CoE-T7
      drs_default_vm_behavior: fullyAutomated
Related