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.
7 Answers
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
- 9,879
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
- 76
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
- 52,255
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 }}'
- 19
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')
- 8,431
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.
- 1,635
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.
- 101,808