W3cubDocs

/Ansible

How to get information from a running virtual machine

Introduction

This section shows you how to collection information from a running virtual machine.

Scenario requirements

You’ve already followed How to run a virtual machine and your virtual machine runs VMware Tools.

How to collect information

In this example, we use the vcenter_vm_guest_* module to collect information about the associated resources.

Filesystem

Here we use vcenter_vm_guest_localfilesystem_info to retrieve the details about the filesystem of the guest. In this example we also use a retries loop. The VMware Tools may take a bit of time to start and by doing so, we give the VM a bit more time.

- name: Get guest filesystem information
  vmware.vmware_rest.vcenter_vm_guest_localfilesystem_info:
    vm: '{{ test_vm1_info.id }}'
  register: _result
  until:
  - _result is not failed
  retries: 60
  delay: 5

Result

{
    "value": [
        {
            "value": {
                "mappings": [],
                "free_space": 774766592,
                "capacity": 2515173376
            },
            "key": "/"
        }
    ],
    "changed": false
}

Guest identity

You can use vcenter_vm_guest_identity_info to get details like the OS family or the hostname of the running VM.

- name: Get guest identity information
  vmware.vmware_rest.vcenter_vm_guest_identity_info:
    vm: '{{ test_vm1_info.id }}'
  register: _result

Result

{
    "value": {
        "full_name": {
            "args": [],
            "default_message": "Red Hat Fedora (64-bit)",
            "id": "vmsg.guestos.fedora64Guest.label"
        },
        "name": "FEDORA_64",
        "ip_address": "192.168.122.242",
        "family": "LINUX",
        "host_name": "localhost.localdomain"
    },
    "changed": false
}

Network

vcenter_vm_guest_networking_info will return the OS network configuration.

- name: Get guest networking information
  vmware.vmware_rest.vcenter_vm_guest_networking_info:
    vm: '{{ test_vm1_info.id }}'
  register: _result

Result

{
    "value": {
        "dns": {
            "ip_addresses": [
                "10.0.2.3"
            ],
            "search_domains": [
                "localdomain"
            ]
        },
        "dns_values": {
            "domain_name": "localdomain",
            "host_name": "localhost.localdomain"
        }
    },
    "changed": false
}

Network interfaces

vcenter_vm_guest_networking_interfaces_info will return a list of NIC configurations.

See also How to attach a VM to a network.

- name: Get guest network interfaces information
  vmware.vmware_rest.vcenter_vm_guest_networking_interfaces_info:
    vm: '{{ test_vm1_info.id }}'
  register: _result

Result

{
    "value": [
        {
            "mac_address": "00:50:56:b3:49:5c",
            "ip": {
                "ip_addresses": [
                    {
                        "ip_address": "192.168.122.242",
                        "prefix_length": 24,
                        "state": "PREFERRED"
                    },
                    {
                        "ip_address": "fe80::b8d0:511b:897f:65a2",
                        "prefix_length": 64,
                        "state": "UNKNOWN"
                    }
                ]
            },
            "nic": "4000"
        }
    ],
    "changed": false
}

Network routes

Use vcenter_vm_guest_networking_routes_info to explore the route table of your vitual machine.

- name: Get guest network routes information
  vmware.vmware_rest.vcenter_vm_guest_networking_routes_info:
    vm: '{{ test_vm1_info.id }}'
  register: _result

Result

{
    "value": [
        {
            "gateway_address": "192.168.122.1",
            "interface_index": 0,
            "prefix_length": 0,
            "network": "0.0.0.0"
        },
        {
            "interface_index": 0,
            "prefix_length": 24,
            "network": "192.168.122.0"
        },
        {
            "interface_index": 0,
            "prefix_length": 64,
            "network": "fe80::"
        },
        {
            "interface_index": 0,
            "prefix_length": 128,
            "network": "fe80::b8d0:511b:897f:65a2"
        },
        {
            "interface_index": 0,
            "prefix_length": 8,
            "network": "ff00::"
        }
    ],
    "changed": false
}

© 2012–2018 Michael DeHaan
© 2018–2021 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/latest/scenario_guides/vmware_rest_scenarios/vm_tool_information.html