# Create multiple VMware guests with a Playbook?

**URL:** <https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [March 10, 2016, 5:00pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831 "2016-03-10T17:00:18Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Matthews](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Mark\_Matthews](https://forum.ansible.com/u/Mark_Matthews)\
**Post date:** [March 10, 2016, 5:00pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/1 "2016-03-10T17:00:18Z")

</div>

Hi

Is it at all possible to create multiple VMware VM’s using an Ansible playbook? Giving each VM a unique name (server1, server2, server3, server4)?

I can see anyway of doing this using the following playbooks, as it keeps failing?

```auto
---
 - hosts: 127.0.0.1
   connection: local
   user: root
   sudo: false
   gather_facts: false
   serial: 1
   vars:
     vcenter_hostname: UK.server.local
     esxhost: xxx.xxx.xxx.xxx
     datastore: UK1
     network: Web
     vmcluster: UKCLUSTER
     guest_name: server1, server2, server3, server4 
     folder: Utilities
     notes: Created by Ansible

   tasks:
    - name: Create VM from template
      vsphere_guest:
        vcenter_hostname: "{{ vcenter_hostname }}"
        username: "{{ username }}"
        password: "{{ password }}"
        guest: "{{ guest_name }}"
        vm_extra_config:
          notes: "{{ notes }}"
          folder: "{{ folder }}"
        from_template: yes
        template_src: "{{ vmtemplate }}"
        cluster: "{{ vmcluster }}"
        resource_pool: "/Resources"
        
        esxi:
          datacenter: UK
          hostname: "{{ esxhost }}"

```

```auto
Any ideas or suggestions would be really appreciated.

```

```auto
Cheers

```

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [March 11, 2016, 10:58am UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/2 "2016-03-11T10:58:25Z")

</div>

Hi Mark,

I’ve not tried this myself with this specific module, but I think you should be able to use one of the with\_ mechanisms in your playbook to create multiple vms.  
Have a look at [http://docs.ansible.com/ansible/playbooks\_loops.html#looping-over-hashes](http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-hashes)

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Mark\_Matthews](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Mark\_Matthews](https://forum.ansible.com/u/Mark_Matthews)\
**Post date:** [March 11, 2016, 1:23pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/3 "2016-03-11T13:23:19Z")

</div>

Hi Jon

Thanks so much for the response.

I was looking at that and have ried a few things without success.

Are you able to see if im on the right track here?  
I have create a file called ‘hostnames’ which is located in a sub directory (files) where the playbook is, and have included the vm names in that file:

```auto
---
serverlist:
  - server1
  - server2

```

I have then added the ‘with\_items’ and variable ‘serverlist’. But i think I may be getting a bit confused through?  
Im I completely off track here?

Playbook:

```auto
---
 - hosts: 127.0.0.1
   connection: local
   user: root
   sudo: false
   gather_facts: false
   serial: 1
   vars:
     vcenter_hostname: UK.server.local
     esxhost: xxx.xxx.xxx.xxx
     datastore: UK1
     network: Web
     vmcluster: UKCLUSTER
     serverlist: files/hostnames 
     folder: Utilities
     notes: Created by Ansible

   tasks:
    - name: Create VM from template
      vsphere_guest:
        vcenter_hostname: "{{ vcenter_hostname }}"
        username: "{{ username }}"
        password: "{{ password }}"
        guest: "{{ list }}"
        vm_extra_config:
          notes: "{{ notes }}"
          folder: "{{ folder }}"
        from_template: yes
        template_src: "{{ vmtemplate }}"
        cluster: "{{ vmcluster }}"
        resource_pool: "/Resources"
       with_items: "{{ serverlist }}
        
        esxi:
          datacenter: UK
          hostname: "{{ esxhost }}"

```

Cheers  
Mark

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [March 14, 2016, 8:52am UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/4 "2016-03-14T08:52:31Z")

</div>

Hi Mark,

I think you are getting pretty close.

I can see a couple of issues in your playbook which might unstick you…

First the with\_items needs to go right at the end of all of the parameters for the vsphere guest module - at the moment you have the exsi parameter after the with\_items.

Also there’s a missing double quote at the end of the with items line:

```auto
with_items: "{{ serverlist }}

should probably be:

with_items: "{{ serverlist }}"

```

The other thing is actually getting your file containing the serverlist variable loaded into Ansible. There’s at least a couple of ways of doing this - either do an include\_vars: file\_containing\_vars.yml at the top of your playbook, or you can pass in the contents of a yaml file on the command line using -e @/path/to/yaml/file.yml. Not sure what is going to suit your usage best

Hope this is enough to get you unstuck.

Jon

---

<div class="post-metadata">

**Author:** ![Mark\_Matthews](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Mark\_Matthews](https://forum.ansible.com/u/Mark_Matthews)\
**Post date:** [April 5, 2016, 11:14am UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/5 "2016-04-05T11:14:49Z")

</div>

Hi Jon

I am still really battling with this…

I have changed the playbook to the following:

Playbook:

---

<div class="post-metadata">

**Author:** ![Nigel\_Metheringham](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nigel\_Metheringham](https://forum.ansible.com/u/Nigel_Metheringham)\
**Post date:** [April 5, 2016, 1:01pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/6 "2016-04-05T13:01:33Z")

</div>

I’m not very familiar with that module, but the iterator on a loop (which is effectively what you have with the with\_items stanza) is “item" and not “list”

So try changing the guest part to:-

guest: “{{ item }”

Nigel.

---

<div class="post-metadata">

**Author:** ![Mark\_Matthews](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Mark\_Matthews](https://forum.ansible.com/u/Mark_Matthews)\
**Post date:** [April 5, 2016, 2:57pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/7 "2016-04-05T14:57:39Z")

</div>

Hi Nigel / Jon

I changed the following as suggessted:

guest: “{{ item }}”

What is happening now is when I run the playbook it is creating one VM in vSphere called “files/hostnames”??

So its as if the playbook is looking at, with\_items: “{{ serverlist }}”, and then seeing the variable, serverlist: files/hostnames, and then creating a VM with that name. Its not looking into the ‘hostnames’ file and creating the multiple VM’s in that file??

Any ideas?

Cheers  
Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Matthews](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Mark\_Matthews](https://forum.ansible.com/u/Mark_Matthews)\
**Post date:** [April 5, 2016, 3:07pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/8 "2016-04-05T15:07:31Z")

</div>

I dont even know if im on the right track here?

Can you guys think of a way that may be easier to do this?

Jon mentioned using looping…but I have looked at this and have no idea how I could apply that to this situation.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [April 5, 2016, 3:14pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/9 "2016-04-05T15:14:31Z")

</div>

Hi Mark,

Quickest way to get going is probably to pass the serverlist file in to ansible using extra vars

ansible-playbook your\_playbook -e @/full/path/to/your/serverlist

If you don’t want to use the -e (extra vars) functionality, then I think you need to use include\_vars, - see [http://docs.ansible.com/ansible/include\_vars\_module.html](http://docs.ansible.com/ansible/include_vars_module.html)

rather than naming the file you want to load in the vars: section of your playbook - vars is just for variables you want to declare within the playbook itself.

Jon

---

<div class="post-metadata">

**Author:** ![Mark\_Matthews](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Mark\_Matthews](https://forum.ansible.com/u/Mark_Matthews)\
**Post date:** [April 5, 2016, 3:16pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/10 "2016-04-05T15:16:15Z")

</div>

Hi guys

Just to let you know, I just worked it out.

I removed the following variable "serverlist: files/hostnames "

And then changed the with\_items to the following:

with\_items:

- ans\_testserver01
- ans\_testserver02

The playbook now creates both VMs (ans\_testserver01 and ans\_testserver02) when it is run.

I dont know if that is the correct way of doing it, but it seems to be working for me.

---

<div class="post-metadata">

**Author:** ![Mark\_Matthews](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Mark\_Matthews](https://forum.ansible.com/u/Mark_Matthews)\
**Post date:** [April 5, 2016, 3:18pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/11 "2016-04-05T15:18:16Z")

</div>

Hi Jon

I will definitely try using your option for extra vars, and see if I can get that to work.

Thanks so much for all your help and assistance with this guys!!

Cheers

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [April 5, 2016, 3:21pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/12 "2016-04-05T15:21:26Z")

</div>

Mark,

Something like this:

`

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [April 5, 2016, 3:27pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/13 "2016-04-05T15:27:18Z")

</div>

Looks like we have crossed messages.

Ansible is pretty flexible, if that suits your need then that’s good enough.

If you use a role you can do away with the include\_vars but putting your serverlist var in a vars/main.yml under your role. Roles are nice as they can magically load lots of stuff like variables - see [http://docs.ansible.com/ansible/playbooks\_roles.html#roles](http://docs.ansible.com/ansible/playbooks_roles.html#roles)

You might find you want to pass in the list of servers with the -e trick too, just depends on how much re-usability you need from your playbook - see [http://docs.ansible.com/ansible/playbooks\_variables.html#passing-variables-on-the-command-line](http://docs.ansible.com/ansible/playbooks_variables.html#passing-variables-on-the-command-line)

HTH

Jon

---

<div class="post-metadata">

**Author:** ![Chun-Hung\_Huang](https://avatars.discourse-cdn.com/v4/letter/c/c89c15/32.png) [@Chun-Hung\_Huang](https://forum.ansible.com/u/Chun-Hung_Huang)\
**Post date:** [June 21, 2016, 12:57pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/14 "2016-06-21T12:57:46Z")

</div>

Hi Mark Matthews and ALL

I meet the same problem with create Create multiple VMware guests with a Playbook.  
Could your share / show your playbook with me?

Or help me to fix my playbook?

My playbook is  
`

- name: Testing vsphere\_guest module in VMware  
hosts: localhost  
connection: local  
vars\_prompt:
- name: “vcenter\_hostname”  
prompt: “Enter vcenter hostname”  
private: no  
default: “vcsa”
- name: “vcenter\_user”  
prompt: “Enter vCenter username”  
private: no  
default: root
- name: “vcenter\_pass”  
prompt: “Enter vcenter password”  
private: yes
- name: “esxi\_hostname”  
prompt: “Enter esxi hostname”  
private: no  
tasks:
- name: Testing gater facts from vSphere  
vsphere\_guest:  
vcenter\_hostname: “{{ vcenter\_hostname }}”  
validate\_certs: no  
username: “{{ vcenter\_user }}”  
password: “{{ vcenter\_pass }}”  
guest: “{{ item }}”  
from\_template: yes  
template\_src: openSUSELeap42.1\_Template  
esxi:  
datacenter: Lab  
hostname: “{{ esxi\_hostname }}”  
with\_items:
- server01
- server02  
`

Error message is

fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “‘item’ is undefined”}

item is undefined.  
But I already have with\_items ??

Thanks

Max

Mark Matthews於 2016年4月5日星期二 UTC+8下午11時16分15秒寫道：

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [June 21, 2016, 7:07pm UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/15 "2016-06-21T19:07:25Z")

</div>

Your with\_items line is indented too far. It should align with the first letter of the word “name”.

---

<div class="post-metadata">

**Author:** ![Chun-Hung\_Huang](https://avatars.discourse-cdn.com/v4/letter/c/c89c15/32.png) [@Chun-Hung\_Huang](https://forum.ansible.com/u/Chun-Hung_Huang)\
**Post date:** [June 22, 2016, 1:30am UTC](https://forum.ansible.com/t/create-multiple-vmware-guests-with-a-playbook/20831/16 "2016-06-22T01:30:04Z")

</div>

Hi James

Thanks 🙂  
It works now

Very thanks for your help

Max

James Tanner於 2016年6月22日星期三 UTC+8上午3時07分49秒寫道：
