user2419802
user2419802

Reputation: 51

Appending one list to another in Ansible (oneliner)

I wanted to append one list to an other in Ansible. While at that, I found, that using

tasks:
    set_fact:
    my_list: "{{ my_list + my_append_list }}"

works but it requires 3 lines of code which seems to be a lot of hassle. The problem is, that using

my_list: "{{ my_list + my_append_list }}"

ends up in an infinite recursion. This type of assignment works only if a third list-name is involved (so something like my_third_list: "{{ my_list + my_append_list }}")

The question is: Is there a way how to append a list to another with a single line of code and without the use of another variable making?

Thanks in advance, P.

Upvotes: 2

Views: 1266

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

No. Because in Ansible in most cases you don't assign variables, but define templates.

Assignment:

- set_fact:
    my_list: "{{ my_list + my_append_list }}"

This is a kind of: template what is on the right side and assign the result to the left side, templating (evaluating) is done at the moment of set_fact execution.

Aliasing (I invented this term, you will not find this in Ansible docs):

vars:
  my_list1: "{{ my_list + my_append_list }}"

This is a kind of: define an alias with name my_list1 (on the left side) for templated string on the right side. The right side will be templated (evaluated) at the moment in time, when you use my_list1 variable in some task.

And if you use the same name on the left side and inside the expression on the right, you get infinite recursion..

Upvotes: 1

Related Questions