简体   繁体   中英

How to pass vars file content in Ansible roles?

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: vms }

As you can see in the playbook, I am trying to create Windows 8.1 VMs but I am unable to figure out how to pass the vmname81 in the roles for the creation of 8.1 VMs. The inputs provided here needs to be used by with_items in the /tasks/main.yml so that I three Windows 8.1 VMs are created. I am also interested to know if this can be done in a better way.

You shouldn't add VM names to a role, role should be generic.

  • Replace with_items: "{{ vmname81 }}" to with_items: "{{ vm_names }}"
  • Move vars ( vmname81 , vmname10 ) to your playbook/hosts/groups vars
  • Apply role like this:

     - hosts: localhost name: Creation of Windows 8.1 VMs roles: - role: vms vm_names: "{{ vmname81 }}" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM