AnsiblePilot — Ansible Automation Platform and Enterprise Ansible

AnsiblePilot covers Red Hat Ansible Automation Platform (AAP) and enterprise Ansible: install and upgrade paths, Event-Driven Ansible, security and day-2 operations, plus Luca Berton's books, videos and training.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 9 technical books published by Apress and Leanpub, 4 of them on Ansible, including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Get VMware vSphere Virtual Machine UUID - Ansible module vmware_guest_info

By Luca Berton · Published 2022-06-24 · Category: troubleshooting

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

Get VMware vSphere Virtual Machine UUID - Ansible module vmware_guest_info

How to Get VMware vSphere Virtual Machine UUID with Ansible?

See also: Gather VMware ESX/ESXi Host Info with Ansible

Ansible Get VMware vSphere Virtual Machine UUID

  • community.vmware.vmware_guest_info
  • Gather info about a single VM
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

  • hostname string / port integer / username string / password string / datacenter string / validate_certs boolean - connection details
  • name string - Virtual machine name
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.

See also: Configure Ansible Dynamic Inventory for VMware in Simple Steps

Playbook

How to Get VMware vSphere Virtual Machine UUID with Ansible.

code

  • vm_uuid.yml
---
- name: vm UUID Playbook
  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.hw_product_uuid
  • vars.yml
---
vcenter_hostname: "vmware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "username@vsphere.local"
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 Playbook] ***************************************************************************************
TASK [include_vars] ***************************************************************************************
ok: [localhost]
TASK [Get VM UUID] ****************************************************************************************
ok: [localhost]
TASK [print VM UUID] **************************************************************************************
ok: [localhost] => {
    "detailed_vm_info.instance.hw_product_uuid": "4225a846-b176-892d-0e27-10a4106269a0"
}
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 Playbook] ***************************************************************************************
TASK [include_vars] ***************************************************************************************
ok: [localhost]
TASK [Get VM UUID] ****************************************************************************************
ok: [localhost]
TASK [print VM UUID] **************************************************************************************
ok: [localhost] => {
    "detailed_vm_info.instance.hw_product_uuid": "4225a846-b176-892d-0e27-10a4106269a0"
}
PLAY RECAP ************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
vSphere

after execution

vmware_guest_info after execution

code with ❤️ in GitHub

See also: Configure a Python Virtual Environment for Ansible VMware - ansible collection community.vmware

Conclusion

Now you know how to Get VMware vSphere Virtual Machine UUID with Ansible.

Category: troubleshooting

Watch the video: Get VMware vSphere Virtual Machine UUID - Ansible module vmware_guest_info — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home