0

I'm configuring a template file in Ansible and I'm having trouble figuring out a way to append values to a list var multiple times.

My setup has multiple groups and roles, each of those could have extra values to append to a global list of routes which is used in a template file.

For example, the route-eth0 template would be:

{% for r in routes_var %}
"{{ r }}"
{% endfor %}

routes_var is defined at the topmost level in roles/common/defaults/main.yml, since every host will have this role applied, this is the base:

routes_var:
  - "1.2.3.4 via {{ gateway }}"
  - "2.3.4.5 via {{ gateway }}"

Then, every other role, group or host should be able to append to this variable so that in the end the full routes file is written with contributions from all of them. The goal is that any groups or roles which need to add routes can do so without having to worry about the rest of them.

For example:

  • roles/logforwarding/defaults/main.yml - extra routes for hosts which forward logs
  • roles/monitored/defaults/main.yml - extra routes for hosts under monitoring
  • group_vars/some_hosts.yml - extra routes for a specific group of hosts
  • group_vars/siteA.yml - extra routes for hosts located in some geographical site
  • etc.

I've tried the following pattern in these files:

routes_var_this:
  - "123.4.5.6 via {{ gateway }}"
  - "234.5.6.7 via {{ gateway }}"

routes_var: "{{ routes_var.extend(routes_var_this) }}"

alternatively (with the same result):

routes_var: "{{ routes_var + routes_var_this }}"

However, both of these solutions fail with a recursion error when applying the template:

(...)
 original message: An unhandled exception occurred while templating '{{ routes_var.extend(routes_var_this) }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: recursive loop detected in template string: {{ routes_var.extend(routes_var_this) }}"}

Is there a way of achieving this with Ansible?

André Fernandes
  • 2,335
  • 3
  • 25
  • 33
  • What is the error message? – Vladimir Botka May 15 '19 at 17:59
  • A huge block of recursing error messages ending in ` original message: An unhandled exception occurred while templating '{{ routes_var.extend(routes_var_this) }}'. Error was a , original message: recursive loop detected in template string: {{ routes_var.extend(routes_var_this) }}"}`. I will add this detail to the question. – André Fernandes May 15 '19 at 18:03
  • 1
    What you are looking to do is not possible when done in the variable definitions. Variable definitions overwrite; they do not append. You will need to have different variable names. – Jack May 15 '19 at 18:03
  • 1
    Take a look at [this answer](https://stackoverflow.com/questions/56110918/in-ansible-how-to-combine-variables-from-active-roles-into-one-array/56112026#56112026) which shows how to incrementally append to list variables. – larsks May 15 '19 at 18:18
  • 1
    Possible duplicate of [In Ansible, how to combine variables from active roles into one array?](https://stackoverflow.com/questions/56110918/in-ansible-how-to-combine-variables-from-active-roles-into-one-array) – larsks May 15 '19 at 18:18

0 Answers0