# deploy VM from template and customize guest

**URL:** <https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115>\
**Category:** Ansible Project\
**Created:** [June 7, 2020, 6:48pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115 "2020-06-07T18:48:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [June 7, 2020, 6:48pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/1 "2020-06-07T18:48:56Z")

</div>

I am trying to deploy VM from VM template with ansible. I got the playbook to deploy the VM. but how do I customize the guest:

I need to

1. rename the guest name to VM name
2. join domain
3. put it in an specific OU

any udea?

---

<div class="post-metadata">

**Author:** ![Dave\_York](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Dave\_York](https://forum.ansible.com/u/Dave_York)\
**Post date:** [June 7, 2020, 10:31pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/2 "2020-06-07T22:31:05Z")

</div>

Ive done this by using the customization argument of the vmware\_guest module to join the domain. but prior to vmware\_guest i use a win\_domain\_computer delegated to a windows utility box to prestage the computer object in the right ou

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [June 8, 2020, 1:59pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/3 "2020-06-08T13:59:02Z")

</div>

I got a customization template in vcenter. how do I specify using that template. Also the template looks like will override my static IP settings on my Vms.

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [June 8, 2020, 2:14pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/4 "2020-06-08T14:14:43Z")

</div>

for ansible, is there a reverse order to detroy the vms after the vm playbook has been run like terraform? Or do I need to create another playbook to destroy the vms?

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [June 8, 2020, 2:20pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/5 "2020-06-08T14:20:19Z")

</div>

> > I’ll assume you are refering to Custom Specs within vCenter :

You can use the following: [https://docs.ansible.com/ansible/latest/modules/vmware\_guest\_module.html](https://docs.ansible.com/ansible/latest/modules/vmware_guest_module.html)

---

<div class="post-metadata">

**Author:** ![David\_Foley](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@David\_Foley](https://forum.ansible.com/u/David_Foley)\
**Post date:** [June 8, 2020, 2:22pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/6 "2020-06-08T14:22:30Z")

</div>

So you want to do something like Terraform init / plan & Apply and once finished you want to do a Terraform Destroy?

No Ansible doesn’t do State Files

---

<div class="post-metadata">

**Author:** ![Dave\_York](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Dave\_York](https://forum.ansible.com/u/Dave_York)\
**Post date:** [June 8, 2020, 4:46pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/7 "2020-06-08T16:46:39Z")

</div>

These are the two plays I use to do what you were asking about:

- name: Ensure Computer Object exists in AD  
win\_domain\_computer:  
name: ‘{{ inventory\_hostname\_short }}’  
dns\_hostname: ‘{{ inventory\_hostname }}’  
domain\_server: domaincontroller.fqdn  
sam\_account\_name: ‘{{ inventory\_hostname\_short }}$’  
ou: ‘{{ AD\_OU }}’  
description: ‘{{ owner\_email }}’  
enabled: yes  
state: present  
delegate\_to: utilityserver.fqdn

- name: Clone to VM from Template  
vmware\_guest:  
hostname: ‘{{ vsphere\_hostname }}’  
username: ‘{{ lookup(“env”, “VMWARE\_USER”) }}’  
password: ‘{{ lookup(“env”, “VMWARE\_PASSWORD”) }}’  
validate\_certs: no  
folder: ‘{{ vmware\_folder }}’  
annotation: “{{ owner\_email }}”  
datacenter: ‘{{ vmware\_datacenter }}’  
name: ‘{{ inventory\_hostname\_short }}’  
cluster: ‘{{ vmware\_cluster }}’  
state: poweredon  
template: ‘{{ vmware\_template }}’  
datastore: ‘{{ vmware\_datastore }}’  
customization:  
domainadmin: ‘{{ lookup(“env”, “ANSIBLE\_NET\_USERNAME”) }}’  
domainadminpassword: ‘{{ lookup(“env”, “ANSIBLE\_NET\_PASSWORD”) }}’  
joindomain: domain.fqdn  
password: ‘{{ templatepwd }}’  
timezone: 004  
disk:

- size\_gb: 100  
type: thin

- size\_gb: 100  
type: thin  
hardware:  
memory\_mb: ‘{{ vmware\_mem }}’  
num\_cpus: ‘{{ vmware\_cpu }}’  
scsi: paravirtual  
hotadd\_cpu: True  
hotremove\_cpu: True  
hotadd\_memory: True  
boot\_firmware: “efi”  
networks:

- name: ‘{{ vmware\_network }}’  
ip: ‘{{ ipv4 }}’  
netmask: ‘255.255.255.0’  
gateway: ‘{{ ipv4\_gateway }}’  
device\_type: vmxnet3  
dns\_servers:

- ‘{{ dns\_primary }}’

- ‘{{ dns\_secondary }}’  
wait\_for\_ip\_address: yes  
wait\_for\_customization: yes  
delegate\_to: localhost  
register: deploy

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [June 8, 2020, 5:06pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/8 "2020-06-08T17:06:28Z")

</div>

Thank you. So for the customization part, i didnt see a mention of the name of the customization template in vcenter.

so ansible does not use that ?

---

<div class="post-metadata">

**Author:** ![Dave\_York](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Dave\_York](https://forum.ansible.com/u/Dave_York)\
**Post date:** [June 8, 2020, 5:12pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/9 "2020-06-08T17:12:51Z")

</div>

I’m not using a customization template, but it may be possible - I think that’s what David Foley was referencing.

Check out customization\_spec within the vmware\_guest module [https://docs.ansible.com/ansible/latest/modules/vmware\_guest\_module.html](https://docs.ansible.com/ansible/latest/modules/vmware_guest_module.html)

You can use the method im using OR you can call customization\_spec

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [June 9, 2020, 2:16pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/10 "2020-06-09T14:16:51Z")

</div>

hi

for

customization:  
domainadmin: ‘{{ lookup(“env”, “ANSIBLE\_NET\_USERNAME”) }}’  
domainadminpassword: ‘{{ lookup(“env”, “ANSIBLE\_NET\_PASSWORD”) }}’

joindomain: domain.fqdn

what does this mean?

lookup(“env”, “ANSIBLE\_NET\_USERNAME”

are you manually putting the username and password in the answer file?

Can this be encrypted

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [June 9, 2020, 3:31pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/11 "2020-06-09T15:31:17Z")

</div>

ok I was able to use the customization\_spec to join the machine to the domain

But how do I assign the statistic IP to the server?

my host file looks like this before using the customization\_spec

[prod-k8s-workers]  
prod-k8s-worker01 deploy\_vsphere\_datastore=‘RW-VA-NIM-VOL9’ guest\_custom\_ip=‘10.200.1.41’ guest\_notes=‘Worker #01’  
prod-k8s-worker02 deploy\_vsphere\_datastore=‘RW-VA-NIM-VOL9’ guest\_custom\_ip=‘10.200.1.43’ guest\_notes=‘Worker #02’  
#prod-k8s-worker03 deploy\_vsphere\_datastore=‘RW-VA-NIM-VOL9’ guest\_custom\_ip=‘10.200.1.44’ guest\_notes=‘Worker #03’  
#prod-k8s-worker04 deploy\_vsphere\_datastore=‘RW-VA-NIM-VOL9’ guest\_custom\_ip=‘10.200.1.45’ guest\_notes=‘Worker #04’  
#prod-k8s-worker05 deploy\_vsphere\_datastore=‘RW-VA-NIM-VOL9’ guest\_custom\_ip=‘10.200.1.46’ guest\_notes=‘Worker #05’

and in my roles file

networks:

- name: ‘{{ guest\_network }}’  
ip: ‘{{ guest\_custom\_ip }}’  
netmask: ‘{{ guest\_netmask }}’  
gateway: ‘{{ guest\_gateway }}’  
customization:  
dns\_servers:
- ‘{{ guest\_dns\_server }}’  
domain : ‘{{ guest\_domain\_name }}’  
hostname: '{{ inventory\_hostname }}

I guess I cannot use the values in my roles file when using customization\_spec?

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [June 11, 2020, 6:09pm UTC](https://forum.ansible.com/t/deploy-vm-from-template-and-customize-guest/33115/12 "2020-06-11T18:09:19Z")

</div>

can i get some help w this? My account to join the domain keeps getting locked out.

This password has spaces in it  
like this  
“This is my pa$$w0rd!”

but account keeps getting locked out
