3

I'm trying to use templates with different sets of variables for each itteration of a determined set of tasks. For example, in one of the tasks I'd like to set specific values for postgres:

- name: Define values for postgres-ds
  template:
    src: postgres-ds.xml.j2
    dest: /opt/ear_{{ instance_control.value }}/postgres-ds.xml
  vars: "{{ postgres_desenv }}"
  notify: Restart Service

In role/vars/main.yaml, I defined:

postgres_desenv:
  var1: somevalue
  var2: someothervalue
  ...

Still, I get the following error:

fatal: [rmt]: FAILED! => {
    "failed": true, 
    "reason": "Vars in a Task must be specified as a dictionary, or a list of dictionaries
    ...

When I try to use the same variable in another context, it works fine:

- debug:
    msg: "{{ item.key }} - {{ item.value }}"
  with_dict: "{{ postgres_desenv }}"

I tried following the answers to this question but I'm still stuck.


My next step is to use a variable to call the variable inside vars, something like:

- name: Define values for postgres-ds
  template:
    src: postgres-ds.xml.j2
    dest: /opt/ear_{{ instance_control.value }}/postgres-ds.xml
  vars: postgres_{{ another_var }}
  notify: Restart Service
Rebeca Maia
  • 438
  • 4
  • 16

3 Answers3

2

You can do something like this:

- name: Define values for postgres-ds
  template:
    src: postgres-ds.xml.j2
    dest: /opt/ear_{{ instance_control.value }}/postgres-ds.xml
  vars:
    settings: "{{ postgres_desenv }}"
  notify: Restart Service

Then within the template you could refer to, e.g.,

{{ settings.var1 }}
larsks
  • 277,717
  • 41
  • 399
  • 399
  • Your answer helped me, I created other fields in the dict that made it easier to refer to the variable later in the code. Instead of a dict I now have a dict of dicts. Thanks. – Rebeca Maia Dec 18 '17 at 14:31
0

In my case, following the answer above, all i had to do is using {{ item.value.(mydictkey) }} and that's it

In my case i defined a global variable like so:

vars:
  vhosts:
    web1
      port: 8080
      dir:  /mywebsite
    web2:
      ...

Then in the task I used:

- name: Render template
  template:        
    src:  "../templates/httpd.vhost.conf.j2"       # Local template
    dest: "/etc/httpd/conf.d/{{ item.key }}.conf"  # Remote destination
    owner: root
    group: root
    mode:  644      
  with_dict: "{{ vhosts }}"

In the template I used:

<VirtualHost *:{{ item.value.port }}>
  DocumentRoot /var/www/{{ item.value.dir }}
</VirtualHost>
Ricky Levi
  • 7,298
  • 1
  • 57
  • 65
-1

If postgres_desenv is defined in vars/main.yml that will be loaded automatically and be available to the role and rest of the playbook. Why do you have to specify that again using "vars" option in the template module task?

Tk Thomas
  • 57
  • 5
  • That would not work for me because I need to be able to change the variables dynamically. The task will be itterated twice if I have two datasources. I'd have to have a way to change the postgres variable (postgres_INSTANCENAME). – Rebeca Maia Dec 18 '17 at 14:28