In this repository you can find examples of how to append things to lists in Ansible.
The original blog post can be found at: https://blog.crisp.se/2016/10/20/maxwenzin/how-to-append-to-lists-in-ansible
The examples make use of the builtin set_fact module.
ansible-playbook demo-append-list.yml
or (if the file is made executable)
./demo-append-list.yml
max@max-x1:~/ansible-append-list$ ansible-playbook demo-append-list.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ******************************************************************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************************************************************************* ok: [localhost] TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "msg": "Append list to list, or merge two lists" } TASK [Setup two lists to be merged] ************************************************************************************************************************************************************************ ok: [localhost] TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "list_one": [ 1, 2, 3 ] } TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "list_two": [ 4, 5, 6 ] } TASK [Merge the two lists] ********************************************************************************************************************************************************************************* ok: [localhost] TASK [Demonstrate merged lists] **************************************************************************************************************************************************************************** ok: [localhost] => { "lists_merged": [ 1, 2, 3, 4, 5, 6 ] } TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "msg": "Append/merge list of maps to list" } TASK [Setup two lists to be merged] ************************************************************************************************************************************************************************ ok: [localhost] TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "list_one": [ { "first_name": "John", "last_name": "Doe" }, { "first_name": "Jane", "last_name": "Doe" } ] } TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "list_two": [ { "first_name": "Douglas", "last_name": "Adams" }, { "first_name": "Frederic", "last_name": "Bastiat" } ] } TASK [Merge the two lists of maps] ************************************************************************************************************************************************************************* ok: [localhost] TASK [Demonstrate merged lists] **************************************************************************************************************************************************************************** ok: [localhost] => { "lists_merged": [ { "first_name": "John", "last_name": "Doe" }, { "first_name": "Jane", "last_name": "Doe" }, { "first_name": "Douglas", "last_name": "Adams" }, { "first_name": "Frederic", "last_name": "Bastiat" } ] } TASK [Initialize an empty list] **************************************************************************************************************************************************************************** ok: [localhost] TASK [Setup a integer variable] **************************************************************************************************************************************************************************** ok: [localhost] TASK [Append item to list] ********************************************************************************************************************************************************************************* ok: [localhost] TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "the_list": [ 5 ] } TASK [Append another item to the list] ********************************************************************************************************************************************************************* ok: [localhost] TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "the_list": [ 5, 6 ] } TASK [Initialize an empty list for our truths] ************************************************************************************************************************************************************* ok: [localhost] TASK [Setup a boolean variable] **************************************************************************************************************************************************************************** ok: [localhost] TASK [Append boolean to list] ****************************************************************************************************************************************************************************** ok: [localhost] TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "my_thruths": [ true ] } TASK [Append another boolean to the list] ****************************************************************************************************************************************************************** ok: [localhost] TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "my_thruths": [ true, false ] } 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": [ "Max" ] } TASK [Append another item to the list] ********************************************************************************************************************************************************************* ok: [localhost] TASK [debug] *********************************************************************************************************************************************************************************************** ok: [localhost] => { "my_strings": [ "Max", "Power" ] } PLAY RECAP ************************************************************************************************************************************************************************************************* localhost : ok=31 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Demo built with Ansible version 2.6.5 Tested up to version 2.10.8