How to use variables while creating VMs using vsphere_guest?

24 views
Skip to first unread message

Chethan S

unread,
Jul 22, 2016, 7:05:54 AM7/22/16
to Ansible Project
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. 

Kai Stian Olstad

unread,
Jul 22, 2016, 11:56:38 AM7/22/16
to ansible...@googlegroups.com
On 22. juli 2016 13:05, Chethan S wrote:
> 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


> ---
> - 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 whould do something like this.

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

*Playbbook:*
---
- hosts: myvms
tasks:
- vsphere_guest:
vcenter_hostname: "nameofhost"
guest: "{{ item }}"
from_template: yes
template_src: "templatename"
validate_certs: no
esxi:
datacenter: dcname
hostname: hname
with_items: play_hosts
delegate_to: localhost


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

vim with this plugin
https://github.com/pearofducks/ansible-vim

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages