I'm a little bit confuse when trying to use the predefined vars into my template. My purpose is I want my template.xml file will contains the right information already defined in the vars yaml file.
For example, for each namevm will have each templates which already contains each information defined in the vars file. But unfortunately, when I run my playbook, I got the following error message that the variable 'vmname' is undefined which I'm confused of.
Do you know which part did I missed?
TASK [createvm : modify db template] ********************************************
fatal: [127.0.0.1]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was : 'list object' has no attribute 'vmname'. The offending line appears to be:\n\n\n- name: modify db template\n ^ here\n" }
What my playbook looks like:
- name: Prepare DB component
hosts: 127.0.0.1
connection: local
roles:
- { role: "createvm" }
What my roles/createvm/tasks/main.yml looks like:
- name: modify db template
template:
src: template.xml
dest: /home/synthesizer/{{ settings.vmname }}.xml
vars:
settings: "{{ dbserver }}"
- name: modify loadbalancer template
template:
src: template.xml
dest: /home/synthesizer/{{ settings.vmname }}.xml
vars:
settings: "{{ loadbalancer }}"
- name: modify forwarder template
template:
src: template.xml
dest: /home/synthesizer/{{ settings.vmname }}.xml
vars:
settings: "{{ forwarder }}"
This is roles/createvm/vars/main.yml looks like:
loadbalancer:
- vmname: elbi1
memory: 1024
cpu: 2
- vmname: elbi2
memory: 2048
cpu: 3
forwarder:
- vmname: efwe1
memory: 1024
cpu: 1
- vmname: efwe2
memory: 4096
cpu: 3
dbserver:
- vmname: dibi1
memory: 1024
cpu: 3
- vmname: dibi2
memory: 2048
cpu: 1
And finally this is what my roles/createvm/templates/templates.xml looks like:
<name>{{settings.vmname}}</name>
<memory unit='KiB'>{{settings.memory}}</memory>
<vcpu placement='static'>{{settings.cpu}}</vcpu>