Ansible Pilot

Get VMware vSphere Virtual Machine UUID - Ansible module vmware_guest_info

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to automate the gathering of UUID of a specific “myvm” VMware vSphere Virtual Machine using Ansible Playbook and vmware_guest_info module.

How to Get VMware vSphere Virtual Machine UUID with Ansible?

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible Get VMware vSphere Virtual Machine UUID

Let’s talk about the Ansible module vmware_guest_info. The full name is community.vmware.vmware_guest_info, which means that is part of the collection of modules to interact with VMware, community-supported. The module’s purpose is to gather info about a single VM.

Parameters

The following parameters are useful in order to Get VMware vSphere Virtual Machine UUID using the module vmware_guest_info. First of all, we need to establish the connection with VMware vSphere or VMware vCenter using a plethora of self-explicative parameters: hostname, port, username, password, datacenter, and validate_certs. Once the connection is successfully established you could specify the virtual machine name to obtain all information about it.

demo

How to Get VMware vSphere Virtual Machine UUID with Ansible. I’m going to show you how to Gather Information about a specific “myvm” VMware Virtual Machine and select the UUID using Ansible Playbook.

code

---
- name: vm UUID demo
  hosts: localhost
  become: false
  gather_facts: false
  collections:
    - community.vmware
  pre_tasks:
    - include_vars: vars.yml
  tasks:
    - name: Get VM UUID
      vmware_guest_info:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ vcenter_datacenter }}"
        validate_certs: "{{ vcenter_validate_certs }}"
        name: "{{ vm_name }}"
      register: detailed_vm_info
    - name: print VM UUID
      ansible.builtin.debug:
        var: detailed_vm_info.instance.instance_uuid
---
vcenter_hostname: "vmware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "[email protected]"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"

execution

$ ansible-playbook vm_uuid.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost
does not match 'all'
PLAY [vm UUID demo] ***************************************************************************************
TASK [include_vars] ***************************************************************************************
ok: [localhost]
TASK [Get VM UUID] ****************************************************************************************
ok: [localhost]
TASK [print VM UUID] **************************************************************************************
ok: [localhost] => {
    "detailed_vm_info.instance.instance_uuid": "502508f4-f36a-1f1f-ebd5-80e0624aa098"
}
PLAY RECAP ************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

idempotency

$ ansible-playbook vm_uuid.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost
does not match 'all'
PLAY [vm UUID demo] ***************************************************************************************
TASK [include_vars] ***************************************************************************************
ok: [localhost]
TASK [Get VM UUID] ****************************************************************************************
ok: [localhost]
TASK [print VM UUID] **************************************************************************************
ok: [localhost] => {
    "detailed_vm_info.instance.instance_uuid": "502508f4-f36a-1f1f-ebd5-80e0624aa098"
}
PLAY RECAP ************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
vSphere

code with ❤️ in GitHub

Recap

Now you know how to Get VMware vSphere Virtual Machine UUID with Ansible. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Access the Complete Book and Learn Quick Ansible by 200+ Examples

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Trustpilot
Follow me

Subscribe not to miss any new releases

June 24, 2022

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress