8

Is there a way to get metadata (i.e. VM Name, Annotations, etc.) from within the Guest OS? I'm using a Ubuntu JeOS template and want to run a script on startup which configures new VMs according to the metadata. This is on VMWare ESX.

Robert Wilson
  • 183
  • 1
  • 3
  • I would like to know the answer for this question as well. I guess you would not know this because the hypervisor would make such information transparent to the guest OS, unless such data would be part of the OEMID of your virtual hardware – A.Rashad May 25 '10 at 09:41

7 Answers7

2

You can set a VM's guestinfo property from the outside (e.g. with govc) and query it from the inside (requiring open-vm-tools):

Outside:

govc vm.change -e 'guestinfo.foo=bar' <yourVM>

Inside:

vmtoolsd --cmd "info-get guestinfo.foo"

Source: https://www.virtuallyghetto.com/2011/01/how-to-extract-host-information-from.html, RedHat's OpenShift on vSphere Installation Guide

fuero
  • 9,413
  • 1
  • 35
  • 40
  • This is what I've used in the past and has worked very well in the wild. Anything in the '.guestinfo." array you can pull from inside the vm using vmware tools on linux and windows, you can write either a boot script or service to poll the data and update the settings accordingly. – Jacob Evans Feb 06 '21 at 15:52
2

I imagine you could use the vSphere SDK for Perl inside your VM to query those items:

http://www.vmware.com/support/developer/viperltoolkit/

You could ask here:

http://communities.vmware.com/community/developer/forums/vsphere_sdk_perl
chamdor
  • 76
  • 1
  • Yeah.. that's what I'm thinking too. Using the Java API (vijava.sf.net) and matching based on the IP address. Not perfect, but it will have to do. – Robert Wilson May 27 '10 at 14:10
1

You can obtain some information (most importan, the VM UUID) by running dmidecode from inside the Guest OS. For example:

[root@localhost ~]# dmidecode
...
Handle 0x0001, DMI type 1, 27 bytes
System Information
        Manufacturer: VMware, Inc.
        Product Name: VMware Virtual Platform
        Version: None
        Serial Number: VMware-42 [REDACTED]
        UUID: [REDACTED]
        Wake-up Type: Power Switch
        SKU Number: Not Specified
        Family: Not Specified
shodanshok
  • 44,038
  • 6
  • 98
  • 162
0

You can install ansible & pyvmomi & then use module vsphere_guest or community.vmware.vmware_guest ( preferred )

https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_guest_module.html

or

https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_vm_info_module.html

---
- hosts: localhost
  gather_facts: false
  connection: local
  vars_files:
    - /etc/ansible/playbooks/vars/vmware_vars_dc1.yml

  tasks:
    - name: Gather VM info 
      community.vmware.vmware_vm_info:
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        validate_certs: no
        vm_type: all
      register: vm_info
    - debug:
        msg: 'VM {{ vm_info }}'
  • If I was to go that way, I would just use PowerCLI and talk to the vSphere instance itself to be frank. Far fewer dependencies. – Mark Henderson Feb 02 '21 at 05:12
  • Yes you can now install powercli in Linux too .. so yes.. so what is your use case ? Setting ansible & pyvmomi is just 2 cmd too .. And with Ansible you get on OS level too which Powercli can not .. so ansible see both world that way .. Few things are easy to do on Powercli & but Ansible can compliment it at cases – user3769149 Feb 08 '21 at 23:48
  • yum install ansible & pip install pyvmomi , so just 2 cmd – user3769149 Feb 08 '21 at 23:50
0

Adding to Fuero's answer, I've used ansible and rc.local previously to configure VMs via guestfacts

ansible vm config

      guestinfo.hostname: "{{ inventory_hostname.split('.')[0] }}"
      guestinfo.fqdn: "{{ inventory_hostname }}"
      guestinfo.domain: "{{ domain }}"
      guestinfo.ip4: "{{ vm_ip4 }}"
      guestinfo.ip6: "{{ vm_ip6 | default(omit) }}"

rc.local

# Set host IP Address
nmcli con mod ens192 ipv4.method manual ipv4.addr "$(vmtoolsd --cmd 'info-get guestinfo.ip4')/24" ipv4.gateway "$(vmtoolsd --cmd 'info-get guestinfo.ip4' | sed 's/\.[0-9]*$/.1/')" ipv4.dns "$(vmtoolsd --cmd 'info-get guestinfo.ip4' | sed 's/\.[0-9]*$/.53/')" ipv4.dns-search $(vmtoolsd --cmd 'info-get guestinfo.domain')

# Set Hostname
nmcli general hostname $(vmtoolsd --cmd 'info-get guestinfo.fqdn')
Jacob Evans
  • 7,636
  • 3
  • 25
  • 55
0

Nowadays instead of vSphere SDK for Perl there is PowerCLI, based on MS's Powershell. You can get any kind of information and change settings regarding VMs, hosts, clusters, and eveything else VMware related.

PowerCLI's VM cmdlets

Krackout
  • 1,559
  • 6
  • 17
0

I'm not aware of any published APIs that allow you to do this with ESX but I am also aware that VMWare have private APIs - fingers crossed they open them up soon.

Chopper3
  • 100,240
  • 9
  • 106
  • 238