The subject of this post is probably terribly named. That said, here’s what I’m trying to accomplish:
I have two VMware vCenter hosts - vcenter1.mycompany.com and vcenter2.mycompany.com. Within each host is are two datacenters - datacenter1 and datacenter2. for each host, the datacenter alignments are vcenter1 – datacenter1 and vcenter2 – datacenter2.
I have a number of playbooks that do actions against vCenter. For example, I have one deletes all snapshots. It looks like this:
Keeping that dictionary update to date will be tedious but could be automated too. Query each vcenter / datacenter pair for its list of VMs and generate a vars file with the above dictionary. Include that vars file in your playbooks. Alternatively (and more expensive) is to create an initial task that queries the each vcenter / datacenter pair and creates this dictionary in real time.
i am quite a newbee with ansible, but i would suggest to
a) use host-variables with e.g. “datacenter” set to the matching datacenter value or
b) use a regex on the host variable to create the datacenter on it
name: Set datacenter variable based on hostname
set_fact:
datacenter: “datacenter{{ ansible_hostname[-1] }}”
when: ansible_hostname | regex_search(‘[1]*[1-2]’)
You can then just use {{ datacenter }} and it should populate correctly. I haven’t tested the above, but if it doesn’t work it should at least put you in the ball park.
Not exactly based on the variables, but I use this to find the datacenter in the vCenter (in my case I have only one datacenter per vCenter so it works)
name: Gather information about all datacenters in vCenter
community.vmware.vmware_datacenter_info:
hostname: “{{ hostname }}”
username: “{{ username }}”
password: “{{ password }}”
validate_certs: “{{ false}}”
delegate_to: localhost
register: datacenterInfo
#- name: Display datacenter Info ansible.builtin.debug: #msg: “{{ datacenterInfo.datacenter_info[0].name }}”
name: set Fact - datacenter
set_fact:
datacenter: “{{ datacenterInfo.datacenter_info[0].name }}”