VmWare Inventory

Howdy,

I’m tasked with creating an ansible playbook that will generate an inventory our our VCenter. I need to be able to get the stats like available and used CPU, available and used memory, available and used disk as well as a count of active VMs in each of my clusters.

I’ve been able to create a playbook using the community.vmware.vmware_cluster_info module that gets CPU, Memory, and Disk information, and I see it can give me a list of the ESXI Hosts in the cluster, but where I’m running into a wall is getting a count of active vms in each cluster, the cluster information module doesn’t seem to provide that. I see that there are a number of vmware_vm_* modules but all of those appear to get information about an individual vm. Since my need is a count of vms something that provides that would be the best solution, but I could do my own count if I can get a list of powered on vms in a cluster, or even a list of all vms in a cluster provided the list also gives me that state of the vm.

On a related topic, I see that community.vmware.vmware_cluster_info module allows you to define the exact properties you want it to return, what baffles me is where do I find a list of the property names? When I call the module under resource_summary I can see that CPU info is provided by cpuCapacityMHz but that to call that in properties the examples shows summary.totalCpu as the property to call. I was able to get what resource_summary calls memCapacityMB with summary.totalMemory following the example of totalCpu, but guessing at the other ones doesn’t result in the same mapping. So, how do I find out what the properties are called? The basic stats are available in the verbose output but that provides more than I’m looking for so if I can tailor my playbook for just the stats I want would be preferred.

Thanks in advance for any assistance.

Regards
-Bob

you can register the output of any task including community.vmware.vmware_cluster_info and then debug the result out

for example:

- name: Gather info from datacenter about specific cluster with tags
  community.vmware.vmware_cluster_info:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    cluster_name: DC0_C0
    show_tag: true
  delegate_to: localhost
  register: cluster_info

- name: print it out to terminal
  ansible.builtin.debug:
    var: cluster_info

Also, consider using the dynamic inventory plugin for vmware Using VMware dynamic inventory plugin — Ansible Documentation to grab everything that is in there

1 Like

+1 for the VMWare dynamic inventory plugin!

1 Like

Hi,

Thanks for the response, I create a playbook based on your recommendation:

- hosts: localhost
  gather_facts: false
  collections:
   - community.vmare
  pre_tasks:
    - include_vars: vars.yml

  tasks:

    - name: collect a list of the datacenters
      community.vmware.vmware_cluster_info:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ generic_auth_username }}"
        password: "{{ ansible_awx_user_pw }}"
        validate_certs: no
        cluster_name: Perf
        show_tag: true
      delegate_to: localhost
      register: cluster_info

    - name: print it out to terminal
      ansible.builtin.debug:
        var: cluster_info

The above is similar to a running playbook that I have which gets inventory, my variables match what’s in my var file. When I run it I get the following error which looks like I have a broken module, is that correct and if so how do I fix?

The full traceback is:

Traceback (most recent call last):
  File "/tmp/ansible_community.vmware.vmware_cluster_info_payload_rs_xiu4o/ansible_community.vmware.vmware_cluster_info_payload.zip/ansible_collections/community/vmware/plugins/module_utils/vmware_rest_client.py", line 30, in <module>
    from com.vmware.vapi.std_client import DynamicID
ModuleNotFoundError: No module named 'com'
fatal: [localhost -> localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "cluster_name": "Perf",
            "datacenter": null,
            "hostname": "vcentererp.cf.cuny.edu",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "port": 443,
            "properties": null,
            "proxy_host": null,
            "proxy_port": null,
            "schema": "summary",
            "show_tag": true,
            "username": "awxadmin",
            "validate_certs": false
        }
    },
    "msg": "Failed to import the required Python library (vSphere Automation SDK) on ansible.cf.cuny.edu's Python /usr/bin/python3.6. See https://code.vmware.com/web/sdk/7.0/vsphere-automation-python for more info. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"

Hey unixcommando can you try reading this post from @Leo Getting error while running a play from AWX 23.2.0 - #3 by Leo because I am struggling to understand your post with the current formatting

Sorry about the formatting, I’m no more familiar with the editor on this forum than I am with Ansible, I copied and pasted what was on the screen and the editor changed it, put dots where hyphens were and changed all the indenting.

Regards
-Bob

1 Like

I fixed it for you @unixcommando - you need 3 backticks ``` not 3 dashes --- :slight_smile:

1 Like

you need to install the vSphere automaiton SDK so that the Ansible VMware modules will work: vSphere Automation SDK for Python - VMware {code}

Can you try installing that and re-running?

Cool thanks. I figured it had to be something like that, but it wasn’t obvious if I could edit the post.

Cheers
-Bob

1 Like

Unfortunatley I already have the plugin.