0

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>

1 Answers1

0

To traverse through vars list you'll have to use loop module so that you can get to individual elements of your list via dot operator which you are using currently.

You'll have to modify your tasks/main.yml as:

---
- name: modify db template
  template:
    src: template.xml
    dest: "/home/synthesizer/ {{ db.vmname }}.xml"
  loop: "{{ dbserver }}"
  loop_control:
    loop_var: db

- name: modify loadbalancer template
  template:
    src: template.xml
    dest: "/home/synthesizer/{{ lb.vmname }}.xml"
  loop: "{{ loadbalancer }}"
  loop_control:
    loop_var: lb

- name: modify forwarder template
  template:
    src: template.xml
    dest: "/home/synthesizer/{{ fw.vmname }}.xml"
  loop: "{{ forwarder }}"
  loop_control:
    loop_var: fw

Now, using this you can traverse through the vars individual lists. Hope this helps!

justjais
  • 344
  • 3
  • 13
  • nice! I think this is what I need. My next question is, how do I define in my template.xml? – Dwi Izaldi Oct 24 '18 at 07:51
  • what exactly you want to define in your template.xml – justjais Oct 24 '18 at 08:06
  • I mean, do I have to create 3 different template.xml (but exactly the same contents) for each db, lb, and fw? because, if I use just one template.xml and define only {{ vmname }}, it will produce another error message saying which vmname to take – Dwi Izaldi Oct 24 '18 at 08:11