I'm building an ansible playbook designed to run on a vcenter VM. It's run on startup by ansible-pull. My goal is to read the tags on the vm using ansible, then make some config changes to the guest system based on those tags.
The difficulty is that I need to read the tags on the guest from inside the guest. The vmware_guest_facts module will let me do this, but it requires the name or uuid of the guest.
Currently the only way I can see to do that is to use vmware_vm_facts to get the facts of ALL the VMs, then find the VM where ansible_default_ipv4.address matches the ip address, set that as a fact, then use vmware_guest_facts to read the tags from that VM by UUID. It seems like overkill.
Is there an easy way for a VM to read its own tags I'm overlooking?
Here's what I have to do now:
- hosts: localhost
connection: local
vars_files:
- vars.yaml
pre_tasks:
- name: Read VMware vm facts
vmware_vm_facts:
hostname: "{{vc_host}}"
password: "{{vc_pass}}"
username: "{{vc_user}}"
validate_certs: no
vm_type: vm
delegate_to: localhost
register: vmfacts
- name: scan for ip
set_fact:
vm_uuid: "{{ item.uuid }}"
vm_name: "{{ item.guest_name }}"
with_items: "{{ vmfacts.virtual_machines }}"
when:
- item.ip_address is defined
- ansible_default_ipv4.address == item.ip_address
- name: Read VMware guest facts
vmware_guest_facts:
datacenter: ASDC
hostname: "{{vc_host}}"
uuid: "{{vm_uuid}}"
password: "{{vc_pass}}"
username: "{{vc_user}}"
tags: yes
validate_certs: no
register: vmguestfacts
- name: set up tags var
set_fact:
vm_tags: "{{ vmguestfacts.instance.tags }}"
- debug:
msg: "{{ vm_tags }}"