How to use variables while creating VMs using vsphere_guest?

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.

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:*