How to append to lists in Ansible

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Since I have found the Ansible documentation to be lacking, and StackOverflow insufficient in this matter, I feel the need to share how you can append to a list using Ansible.

I’ve created a demonstration playbook and published it on GitHub. See:

How to merge two lists together:

- debug: msg="Append list to list, or merge two lists"

- name: Setup two lists to be merged
      - 1
      - 2
      - 3
      - 4
      - 5
      - 6

- name: Merge the two lists
    lists_merged: "{{ list_one }} + {{ list_two }}"

- name: Demonstrate merged lists
  debug: var=lists_merged

The output will be:

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "Append list to list, or merge two lists"

TASK [Setup two lists to be merged] ********************************************
ok: [localhost]

TASK [Merge the two lists] *****************************************************
ok: [localhost]

TASK [Demonstrate merged lists] ************************************************
ok: [localhost] => {
    "lists_merged": [

Appending a single item to a list is trickier, especially when it’s a string. Here’s how to do it:

- name: Initialize an empty list for our strings
    my_strings: []

- name: Setup a string variable
    my_name: "Max"

- name: Append string to list
    my_strings: "{{ my_strings }} + [ '{{ my_name }}' ]"

- debug: var=my_strings

- name: Append another item to the list
    my_strings: "{{ my_strings }} + [ 'Power' ]"

- debug: var=my_strings

The output will be:

TASK [Initialize an empty list for our strings] ********************************
ok: [localhost]

TASK [Setup a string variable] *************************************************
ok: [localhost]

TASK [Append string to list] ***************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "my_strings": [

TASK [Append another item to the list] *****************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "my_strings": [

More examples can be found at Happy provisioning!


  • 1
    2017-07-14 - 16:36 | Permalink

    Good stuff. Ansible / jinja’s syntax can be mind boggling at times and this was a real help. Although I found I preferred surrounding the whole expression with {{ }} instead of the individual elements. So for strings:

    – set_fact:
    my_strings: “{{ my_strings + [ my_name ] }}”

    – set_fact:
    my_strings: “{{ my_strings + [ ‘Power’ ] }}”

    This way you know that everything between the {{ }} is jinja so you don’t have to keep mentally switching back and forth between yaml and jinja syntax as you read along the line.

    In case any other readers are wondering, here’s how to append a dict created on-the-fly to a list of dicts:

    – set_fact:
    my_dicts: “{{ my_dicts + [ { ‘name’: ‘fred’, ‘age’: computed_age } ] }}”

    You can put quoted strings or variables as keys and values and the variables will be dereferenced properly. Those variables can even be other lists and dicts if you’re assembling some complex data.

    • 2
      2018-04-17 - 17:09 | Permalink


      For some reason your coding worked, but OP’s didn’t.

      In my case with Ansible 2.5:

      This worked:

      my_strings: “{{ my_strings + [ ‘Power’ ] }}”

      This did not worked:

      my_strings: “{{ my_strings }} + [ ‘{{ my_name }}’ ]”

      thank you for your help guys! this post (and it’s answers) were most helpfull!

  • 3
    2017-10-02 - 21:46 | Permalink

    this was very helpful and accurate. thanks!

  • 4
    2017-11-08 - 19:03 | Permalink


    I have list of servers IPS using dynamic json file , How can i execute command say ping to all servers from list …

    blow is sample data from file …


    • 5
      2017-11-16 - 08:22 | Permalink

      Hi Hemant!

      I don’t have the time to write the code right now, but I see two different approaches.

      Your list of servers from a json file could be viewed as what Ansible would refer to as a “dynamic inventory”. See:

      The other approach would be to take the json file, parse it into a list, probably using a jinja2 filter, and then iterate on that list using `with_items` and call whatever module you want (like command or ping).

  • 6
    2017-11-16 - 08:41 | Permalink

    Hi again Hemant!

    I decided to try coding it. This is the latter approach above. See:

  • 7
    John Qualls
    2017-12-15 - 20:27 | Permalink

    Thanks for your help on this Max. I agree that Ansible’s documentation is not helpful about this concept.

  • 8
    2018-01-22 - 09:38 | Permalink

    Thank you a lot for this post. I am a newbie and started to use anslible not much time ago so I don’t know yaml syntax pretty well. Really, it is quite useful for me.

  • 9
    2018-11-02 - 12:25 | Permalink

    Hi Max i am facing an issue where lets say i have a list [“abc”, “def”] i have to loop through the list & get some data which is also a list of dictionaries [{“name”: “value”},{“name”, “value”}] & concatenate them into a single list.

    i am not able to concatenate the lists. can you help me ?

    • 10
      2018-11-17 - 10:07 | Permalink

      Not sure if I understand your question correctly, but if you have a list of dictionaries and you want to filter out a single attribute (like “name”) from the dictionary and make that a list you could do this:

      {{ my_list_of_dicts | map(attribute=’name’) | list }}

  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.