# How to use variables while creating VMs using vsphere\_guest?

**URL:** <https://forum.ansible.com/t/how-to-use-variables-while-creating-vms-using-vsphere-guest/22084>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [July 22, 2016, 11:05am UTC](https://forum.ansible.com/t/how-to-use-variables-while-creating-vms-using-vsphere-guest/22084 "2016-07-22T11:05:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chethan\_S](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Chethan\_S](https://forum.ansible.com/u/Chethan_S)\
**Post date:** [July 22, 2016, 11:05am UTC](https://forum.ansible.com/t/how-to-use-variables-while-creating-vms-using-vsphere-guest/22084/1 "2016-07-22T11:05:54Z")

</div>

I am in the process of developing a playbook to create VMs in VMware vCenter Server wherein I need to be able to name the VMs with incremental number suffixes - VM(x) becomes VM1, VM2. Since the number of VMs would be fixed, I intend to specify the number in the playbook itself.

I’m assuming I need to specify the highlighted variable vmname (below) prior to the vsphere\_guest module.

> ## `
> 
> - name: Create VMs on VMware vCenter Server  
> hosts: localhost
> 
> tasks:
> 
> - vsphere\_guest:  
> vcenter\_hostname: “nameofhost”  
> guest: “{{ vmname }}”  
> from\_template: yes  
> template\_src: “templatename”  
> validate\_certs: no  
> esxi:  
> datacenter: dcname  
> hostname: hname  
> `

I would also like to know which kind of editor you people use to write Ansible Playbooks.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 22, 2016, 3:56pm UTC](https://forum.ansible.com/t/how-to-use-variables-while-creating-vms-using-vsphere-guest/22084/2 "2016-07-22T15:56:26Z")

</div>

> I am in the process of developing a playbook to create VMs in VMware  
> vCenter Server wherein I need to be able to name the VMs with incremental  
> number suffixes - VM(x) becomes VM1, VM2. Since the number of VMs would be  
> fixed, I intend to specify the number in the playbook itself.
> 
> I'm assuming I need to specify the highlighted variable vmname (below)  
> prior to the vsphere\_guest module.

What you are looking for i loop, read all about it here  
[https://docs.ansible.com/ansible/playbooks\_loops.html](https://docs.ansible.com/ansible/playbooks_loops.html)

> ---  
> - name: Create VMs on VMware vCenter Server  
> &nbsp;&nbsp;&nbsp;hosts: localhost
> 
> &nbsp;&nbsp;&nbsp;tasks:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- vsphere\_guest:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vcenter\_hostname: "nameofhost"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guest: "{{ vmname }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from\_template: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template\_src: "templatename"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate\_certs: no  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esxi:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datacenter: dcname  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: hname

I whould do something like this.

\*Inventory file:\*  
[myvms]  
VM[1:20]

\*Playbbook:\*
