# \[ISSUE\] Ansible for VMware, deploy vms and update them

**URL:** <https://forum.ansible.com/t/issue-ansible-for-vmware-deploy-vms-and-update-them/37024>\
**Category:** Ansible Project\
**Tags:** vmware, ubuntu\
**Created:** [August 23, 2022, 3:08pm UTC](https://forum.ansible.com/t/issue-ansible-for-vmware-deploy-vms-and-update-them/37024 "2022-08-23T15:08:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheReal\_MVP](https://avatars.discourse-cdn.com/v4/letter/t/59ef9b/32.png) [@TheReal\_MVP](https://forum.ansible.com/u/TheReal_MVP)\
**Post date:** [August 23, 2022, 3:08pm UTC](https://forum.ansible.com/t/issue-ansible-for-vmware-deploy-vms-and-update-them/37024/1 "2022-08-23T15:08:32Z")

</div>

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**

# config file for ansible – [http://ansible.com/](http://ansible.com/)

# ==============================================

# 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

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 23, 2022, 5:29pm UTC](https://forum.ansible.com/t/issue-ansible-for-vmware-deploy-vms-and-update-them/37024/2 "2022-08-23T17:29:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paul\_Manno](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/paul_manno/32/2804_2.png) [@Paul\_Manno](https://forum.ansible.com/u/Paul_Manno)\
**Post date:** [August 23, 2022, 5:41pm UTC](https://forum.ansible.com/t/issue-ansible-for-vmware-deploy-vms-and-update-them/37024/3 "2022-08-23T17:41:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TheReal\_MVP](https://avatars.discourse-cdn.com/v4/letter/t/59ef9b/32.png) [@TheReal\_MVP](https://forum.ansible.com/u/TheReal_MVP)\
**Post date:** [August 23, 2022, 5:51pm UTC](https://forum.ansible.com/t/issue-ansible-for-vmware-deploy-vms-and-update-them/37024/4 "2022-08-23T17:51:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![D111](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@D111](https://forum.ansible.com/u/D111)\
**Post date:** [August 23, 2022, 6:03pm UTC](https://forum.ansible.com/t/issue-ansible-for-vmware-deploy-vms-and-update-them/37024/5 "2022-08-23T18:03:17Z")

</div>

Just use terraform

---

<div class="post-metadata">

**Author:** ![TheReal\_MVP](https://avatars.discourse-cdn.com/v4/letter/t/59ef9b/32.png) [@TheReal\_MVP](https://forum.ansible.com/u/TheReal_MVP)\
**Post date:** [August 23, 2022, 11:15pm UTC](https://forum.ansible.com/t/issue-ansible-for-vmware-deploy-vms-and-update-them/37024/6 "2022-08-23T23:15:20Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![TheReal\_MVP](https://avatars.discourse-cdn.com/v4/letter/t/59ef9b/32.png) [@TheReal\_MVP](https://forum.ansible.com/u/TheReal_MVP)\
**Post date:** [August 24, 2022, 4:07am UTC](https://forum.ansible.com/t/issue-ansible-for-vmware-deploy-vms-and-update-them/37024/7 "2022-08-24T04:07:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mike\_Tipton](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@Mike\_Tipton](https://forum.ansible.com/u/Mike_Tipton)\
**Post date:** [November 7, 2022, 10:05pm UTC](https://forum.ansible.com/t/issue-ansible-for-vmware-deploy-vms-and-update-them/37024/8 "2022-11-07T22:05:39Z")

</div>

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
