0
votes

I have my ansible playbook which basically create a new Virtual machine from template in esxi what , i have to convert the playbook into roles. I am new to ansible so i don't know how to call my hard coded variables from roles(install)->vars directory .

This is my playbook

---
# create a new VM from a template

- name: VM from template
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
    vcenter_hostname: vcenter-app
    vcenter_user: john@doe
    vcenter_pass: blabla6
    esxhost: esx-4.cbalo.fr
    datastore: VM-PROD-02-NORMAL
    vmtemplate: Centos7-template
    name: "newvm2"
    notes: Ansible Test
    dumpfacts: False
  tasks:
    - name: Create VM from template
      vmware_guest:
        validate_certs: False
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_pass }}"
        esxi_hostname: "{{ esxhost }}"
        datacenter: CD06
        folder: Test
        name: "{{ name }}"
        template: "{{ vmtemplate }}"
        hardware:
          memory_mb: "{{ vm_memory | default(1024) }}"
        wait_for_ip_address: True
        state: present
      register: newvm2

I have divide this into my role(install)->tasks->main.yml like this

---
- name: Create VM from template
              vmware_guest:
                validate_certs: False
                hostname: "{{ vcenter_hostname }}"
                username: "{{ vcenter_user }}"
                password: "{{ vcenter_pass }}"
                esxi_hostname: "{{ esxhost }}"
                datacenter: CD06
                folder: Test
                name: "{{ name }}"
                template: "{{ vmtemplate }}"
                hardware:
                  memory_mb: "{{ vm_memory | default(1024) }}"
                wait_for_ip_address: True
                state: present
              register: newvm2

Then in my main directory i create new file run.yml and include my role

---
    # create a new VM from a template
    
    - name: VM from template
      hosts: localhost
      gather_facts: false
      connection: local
      
      roles:
        - install

The issue is i don't know how to call vars from roles->vars directory as you can see in my playbook i have hard code variables. I need to get or set the variable in file and call it from that file.

**vcenter_hostname: vcenter-app
vcenter_user: john@doe
vcenter_pass: blabla6
esxhost: esx-4.cbalo.fr
datastore: VM-PROD-02-NORMAL
vmtemplate: Centos7-template
name: "newvm2"
datacenter: CD06
folder: Test**
1

1 Answers

0
votes

Variables in ansible are a set of values that will replace certain place-holders. You supply them when running your playbook and they will "trickle down" if you include roles etc. Check the documentation to find out about variable precedence in ansible.

So what you want to do is to put your variable-assignments in your inventory (you can split out variables in separate files as well, using group_vars) and then use the -i path/to/inventory.yml on your ansible-playbook command.

A structure could look like that:

my-ansible/
  roles/
    my-role/
      tasks/
        main.yaml
      defaults/
        main.yaml # contains default values for your variables
  inventory/
    inventory.yaml # contains hosts and variable assignments
  run.yaml
  

Then, you can run it like that:

ansible-playbook -i inventory/inventory.yaml run.yaml

Hints:

  • Don't use my-role/vars as it is very hard to override variable assignments in there. (See variable precedence)
  • defaults/main.yaml is not necessary, but can be handy for variables you rarely change