How to specify variables to be used in roles?

41 views
Skip to first unread message

Chethan S

unread,
Sep 23, 2016, 5:25:29 AM9/23/16
to Ansible Project
I am in the process of migrating to Ansible roles model and I have the following structure - 

../roles/vms/tasks/main.yml
---
# To Create VMs on the VMware vCenter Server
 
- name: Creation of Windows 8.1 VMs
    vsphere_guest
:
      vcenter_hostname
: "name"
      guest
: "{{ item }}"
      from_template
: yes
      template_src
: "templatename"
      validate_certs
: no
      esxi
:
        datacenter
: dc
        hostname
: hname
    with_items
: "{{ vmname81 }}"

../roles/vms/vars/main.yml
---
vmname81
:
 
- Client1
 
- Client2
 
- Client3


vmname10
:
 
- Client4
 
- Client5


playbook.yml
---
# To Create VMs on the VMware vCenter Server
- hosts: localhost
  name
: Creation of Windows 8.1 VMs
  roles
:
   
- { role: vmcreation }

As you can see in the playbook, I am trying to create Windows 8.1 VMs and I am unable to figure out how to pass the vmname81 in the roles for the creation of 8.1 VMs. I am also interested to know if this can be done in a better way.

ZillaYT

unread,
Sep 23, 2016, 2:14:00 PM9/23/16
to Ansible Project
What is your file structure? You show a vms role, but not a vmcreation role, but yet your playbook includes the vmcreation role?

ZillaYT

unread,
Sep 23, 2016, 2:36:58 PM9/23/16
to Ansible Project
I think I know what you're after. Try this.

Have two files in ../roles/vms/vars, namely,

../roles/vms/vars/vmname81.yml
---
windows_vms
:

 
- Client1
 
- Client2
 
- Client3


...and ...

../roles/vms/vars/vmnames10.yml
---
windows_vms
:
 
- Client4
 
- Client5

Then your role will be this. Note include_vars line, and the "with_items" change

../roles/vms/tasks/main.yml
---
# To Create VMs on the VMware vCenter Server

 
- include_vars: "vars/windows{{ windows_ver }}.yml"


 
- name: Creation of Windows 8.1 VMs
    vsphere_guest
:
      vcenter_hostname
: "name"
      guest
: "{{ item }}"
      from_template
: yes
      template_src
: "templatename"
      validate_certs
: no
      esxi
:
        datacenter
: dc
        hostname
:
hname
    with_items
: "{{ windows_vms }}"



Then

playbook.yml
---
# To Create VMs on the VMware vCenter Server
- hosts: localhost
  name
: Creation of Windows 8.1 VMs
  roles
:

   
- { role: vms, windows_ver: 81 } # to create the windows in vars/windows81.yml
    - { role: vms, windows_ver: 10 } # to create the windows in vars/windows10.yml




Make sense?


On Friday, September 23, 2016 at 5:25:29 AM UTC-4, Chethan S wrote:

ZillaYT

unread,
Sep 23, 2016, 2:40:52 PM9/23/16
to Ansible Project
Sorry, the include_vars should be:

- include_vars: "vars/vmname{{ windows_ver }}.yml"

Chethan S

unread,
Sep 23, 2016, 10:28:32 PM9/23/16
to Ansible Project
Thank you, Zilla. I got a simpler solution for this question which I posted at Stack Overflow wherein I didn't have to split vars files. I have had success following it. 
Reply all
Reply to author
Forward
0 new messages