Hi there,
I’ve been banging my head with this for some time now and I can’t figure it out.
I’m using Ansible the vmware commnity plugins to deploy 4 VMs from a template and they all have DHCP running. Ultimately, I want to be able to grab the IPs from all 4 VMs, connect to them and run some commands (possibly update them and push my main ansible ssh key).
config.yml
vcenter_hostname: ‘FQDN-of-my-vcenter’
vcenter_username: ‘administrator@vsphere.local’
vcenter_password: ‘MyPassword’
vcenter_datastore: ‘Storage’
vcenter_datacenter: ‘Datacenter’
vcenter_folder: ‘deployments/ubuntu’
vcenter_datastore: ‘Storage’
guest_id: ‘Ubuntu64’
guest_network_1: ‘VM Network’
guest_network_2: ‘Docker’
guest_wait_for_ip_address: ‘yes’
guest_state: ‘poweredon’
- Prepare VMs information
machine_user: user
machine_initial_user: root
machine_initial_password: P@ssw0rdP@ssw0rd
ansible.cfg
==============================================
nearly all parameters can be overridden in ansible-playbook
or with command line flags. ansible will read ANSIBLE_CONFIG,
ansible.cfg in the current working directory, .ansible.cfg in
the home directory or /etc/ansible/ansible.cfg, whichever it
finds first
[defaults]
some basic default values…
library = ./library
additional paths to search for roles in, colon separated
roles_path = ./roles
[inventory]
#Nothing in here
My playbook
deploy-vm.yaml
root@user-ubuntu:/opt/ansible/multiple_vm# more deploy-vm.yaml
in our env we have an ansible tower workflow where we create the VM, register the results, then send the mac address of the new VM off to InfoBlox to create a new DHCP record. Once InfoBlox creates the DHCP record it sees the DHCP broadcasts from the VM and responds. This gives the VM all it needs to get on the network. At that point we have a DHCP record with FQDN and a VM on the network. Now we can reference the machine by FQDN.
Hello,
Don’t get me wrong, I love ansible, and I realize this is an ansible mailing group, but IMO Terraform is a better tool for this.
I build a whole workflow around packer and ansible glued together with Python and AWX in the mix and it’s really, really hard to orchestrate a lot of this stuff reliably. After just a few weeks using Terraform, things are coming together much more easily and with much more stability.
I’m using Terraform to clone the VMs, then using a terraform template to render out an inventory file and a provisioner tied to the template that launches ansible on the VM. It’s super clean, super fast, does not require waiting on AWX inventory to catch up or process and it’s very simple to create/modify/delete infrastructure with this (something not really trivial with pure Ansible).
My 2 cents…
Best,
Paul
Walter: Oh that would be the dream! Register the mac address with the DHCP server, then wait for the VM to pick up the ip.
Paul: I was looking at terraform as well, but I haven’t really searched how well it was all working together. Do you have any off-hand scripts I could follow and check out?
Thanks to both!
I’m getting close
I created this playbook after finding the vmware_vm_info module
- hosts: all
gather_facts: false
become: false
tasks:
- name: Gathering info from vms from ‘{{ folder }}’
community.vmware.vmware_vm_info:
hostname: 192.168.1.51
username: administrator@vsphere.local
password: password
validate_certs: False
folder: “/Datacenter/vm/deployments/ubuntu”
delegate_to: localhost
register: vm_info
- debug:
msg: “{{ vm_info }}”
I’ll get a dump of information from all 4 machines inside the folder specified.
Now I need to find a way to extract the IP from it and save it somewhere to use it to connect to them
I’ll keep adding with my findings, might save people some time.
Wrote playbook:
- hosts: localhost
gather_facts: false
vars_files:
- all_config.yml
tasks:
- name: Gathering info from vms
community.vmware.vmware_vm_info:
hostname: “{{ vcenter_hostname }}”
username: “{{ vcenter_username }}”
password: “{{ vcenter_password }}”
validate_certs: False
folder: “/Datacenter/vm/deployments/ubuntu”
delegate_to: localhost
register: vm_info
- debug:
var: name_ip
vars:
name_ip: “{{ vm_info.virtual_machines|
items2dict(key_name=‘guest_name’,
value_name=‘ip_address’) }}”
which will spit out this:
TASK [debug] ************************************************************************************************************************************
ok: [localhost] => {
“name_ip”: {
“Ubuntu 22.04 VDI Template”: “192.168.1.122”,
“server01”: “192.168.1.181”,
“server02”: “192.168.1.114”,
“server03”: “192.168.1.117”
}
}
I think i’m getting close. Took me a whole 3 hours to figure this part.
Now I need to save this into the ansible inventory (I think?) and run the apt update and apt upgrade against those vms.
I was wondering if you figured out the “I’ll need to right-click on each VMs and set the network to “connected” since it doesn’t do this automatically, i’ll need to figure this out.” part and update you with my findings as I ran into this as well.
It was as simple as adding perl to my base template as it is needed to do the customization after deployment.
Cheers,
Tipton