# VMware - convert template to virtual machines

**URL:** <https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [April 27, 2018, 1:55pm UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397 "2018-04-27T13:55:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edward\_Ahern](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@Edward\_Ahern](https://forum.ansible.com/u/Edward_Ahern)\
**Post date:** [April 27, 2018, 1:55pm UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/1 "2018-04-27T13:55:54Z")

</div>

Hi All,

I’m looking for a way to convert a VMware template to a virtual machine using Ansible.  
The work flow is:  
Convert a template to a virtual machine  
Power on  
Login to the virtual machine  
Update the OS patches (Linux)  
Power off  
Convert Virtual machine back to template

As you can see I want to keep the patch levels up to date on my templates and do all the above in Ansible.  
I’ve searched for ways to do this but haven’t come up with anything.

Any suggestions are most welcome.

Thanks,

Ted

---

<div class="post-metadata">

**Author:** ![EspressoBeanies](https://avatars.discourse-cdn.com/v4/letter/e/ea666f/32.png) [@EspressoBeanies](https://forum.ansible.com/u/EspressoBeanies)\
**Post date:** [April 27, 2018, 3:04pm UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/2 "2018-04-27T15:04:49Z")

</div>

I don’t think you can technically do that from the current list of available modules: [http://docs.ansible.com/ansible/latest/modules/list\_of\_cloud\_modules.html](http://docs.ansible.com/ansible/latest/modules/list_of_cloud_modules.html)

I popped an earlier question about how ESXi transfers IP address settings but no one got back to me. Some of the network settings apply only to Windows-based VMs.

---

<div class="post-metadata">

**Author:** ![Sebastien\_Desbois](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastien\_Desbois](https://forum.ansible.com/u/Sebastien_Desbois)\
**Post date:** [April 27, 2018, 4:33pm UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/3 "2018-04-27T16:33:20Z")

</div>

Hello,

You can’t do that with only ansible modules

Here is the solution we have found to do this :

- Convert template to VM : use ansible to execute a powercli script to do that. (even more easy now with Linux powercli)
- Power on the VM with module vsphere\_guest (yes, old module but working really fine)
- Get VMfacts with module vsphere\_guest (need that to get the IP of the VM - assumption : that the VM has a network cardwith either a fixed IP or in DHCP)
- Put VM info in in-memory inventory (espcially the IP)
- Log to the VM and update it through standard ansible modules (you can do either linux or windows VMs like that)
- Shut down the VM with a powercli script with a Stop-VMGuest cmdlet (and in this script loop until you get the VM state as powered off) – use of vpshere\_guest is not recommanded to stop the VM because it’s not doing a proper shutdown at OS level
- Convert back the VM to template with a powercli script

It may be a little complexe but it’s working fine.  
Maybe some of this can be done with vmware\_guest and vmware\_guest\_facts, but I don’t like the current version of those modules. I had too many bugs with them.

Best Regards,

Sebastien

---

<div class="post-metadata">

**Author:** ![Edward\_Ahern](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@Edward\_Ahern](https://forum.ansible.com/u/Edward_Ahern)\
**Post date:** [April 27, 2018, 8:54pm UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/4 "2018-04-27T20:54:40Z")

</div>

Hi Sebastien,

Thanks very much for the comprehensive reply. Would you be able to share any of those scripts / playbooks you mentioned. I’ve no familiarity with powercli and have yet to dabble with VMware modules in ansible.

Anything would be greatly appreciated just to get me started.

Thanks again,

Ted

---

<div class="post-metadata">

**Author:** ![Nick\_Rogers](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nick\_Rogers](https://forum.ansible.com/u/Nick_Rogers)\
**Post date:** [April 27, 2018, 10:00pm UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/5 "2018-04-27T22:00:32Z")

</div>

I do all of this with playbooks and the Ansible vmware\_guest module with Ansible 2.4.4 against vCenter currently - though I am not sure the status of the vmware\_guest module going forward (there are Github issues in 2.5 that discuss removing/depracating it).

I have the following structure:

`

project

- inventory  
… other inventory dirs …
- vms
- group\_vars  
all.yml
- host\_vars  
rhel75x64-base.yml  
rhel75x64-updated.yml  
hosts
- playbooks  
… other playbook dirs …
- vcenter  
create\_snapshots.yml  
revert\_snapshots.yml  
update\_template.yml
- roles  
… other role dirs …
- vcenter
- tasks  
create\_snapshot.yml  
create\_template.yml  
create\_vm.yml  
delete\_vm.yml  
refresh\_vm.yml  
revert\_snapshot.yml  
shutdown\_vm.yml  
start\_vm.yml
- vars  
main.yml

`

We have the following set up on a schedule:  
playbooks/vcenter/update\_template.yml gets run weekly and creates a vm in our vcenter, updates it from our yum repo, and turns it into a template  
playbooks/vcenter/create\_snapshots.yml gets run weekly (1 hour later than update\_template) that destroys a vcenter inventory, creates a new set in vcenter, and creates snapshots so they are ready for CI builds

If you’d like more info, I can thow it in a Github for you to peruse, but will be a day or so.

---

<div class="post-metadata">

**Author:** ![Edward\_Ahern](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@Edward\_Ahern](https://forum.ansible.com/u/Edward_Ahern)\
**Post date:** [April 28, 2018, 10:32am UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/6 "2018-04-28T10:32:09Z")

</div>

Hi Nick,

Thanks for the reply. If you could put it up on Github that would be brilliant.

Thanks again,

Ted

---

<div class="post-metadata">

**Author:** ![Nick\_Rogers](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nick\_Rogers](https://forum.ansible.com/u/Nick_Rogers)\
**Post date:** [May 1, 2018, 2:24am UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/7 "2018-05-01T02:24:57Z")

</div>

Ok, here you go, it’s still in progress, but it’s up.

[https://github.com/nickrnet/ansible-vmware](https://github.com/nickrnet/ansible-vmware)

---

<div class="post-metadata">

**Author:** ![Edward\_Ahern](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@Edward\_Ahern](https://forum.ansible.com/u/Edward_Ahern)\
**Post date:** [May 1, 2018, 8:19am UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/8 "2018-05-01T08:19:07Z")

</div>

Cheers Nick,

Plenty there to get stuck into there, appreciate it

Ted

---

<div class="post-metadata">

**Author:** ![talha\_jilal](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@talha\_jilal](https://forum.ansible.com/u/talha_jilal)\
**Post date:** [May 3, 2018, 2:46pm UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/9 "2018-05-03T14:46:55Z")

</div>

Hello Folks,

with ansible 2.5 we can control cpu cores but not cpu virutal number of sockets. Any idea ?

Thanks

---

<div class="post-metadata">

**Author:** ![Sebastien\_Desbois](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastien\_Desbois](https://forum.ansible.com/u/Sebastien_Desbois)\
**Post date:** [May 15, 2018, 1:27pm UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/10 "2018-05-15T13:27:04Z")

</div>

Hello,

I will try to put it in a github

---

<div class="post-metadata">

**Author:** ![Edward\_Ahern](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@Edward\_Ahern](https://forum.ansible.com/u/Edward_Ahern)\
**Post date:** [May 15, 2018, 3:45pm UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/11 "2018-05-15T15:45:09Z")

</div>

Thanks Sebastian

---

<div class="post-metadata">

**Author:** ![Nick\_Rogers](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nick\_Rogers](https://forum.ansible.com/u/Nick_Rogers)\
**Post date:** [May 15, 2018, 6:58pm UTC](https://forum.ansible.com/t/vmware-convert-template-to-virtual-machines/27397/12 "2018-05-15T18:58:28Z")

</div>

It looks to me like “num\_cpus” is number of sockets, and “num\_cpu\_cores\_per\_socket” can set the cores per socket.

Is that what you’re asking? or am I way off the mark… If you don’t specify “num\_cpu\_cores\_per\_socket”, it defaults to 1, if I’m reading the pyvmomi source correctly: [https://github.com/vmware/pyvmomi/blob/575ab56eb56f32f53c98f40b9b496c6219c161da/docs/vim/vm/VirtualHardware.rst](https://github.com/vmware/pyvmomi/blob/575ab56eb56f32f53c98f40b9b496c6219c161da/docs/vim/vm/VirtualHardware.rst)
