How to merge multiple lists in ansible?

How to merge multiple lists in ansible?

We can use “+’ to merge the two different lists in an ansible playbook.

We are going to see the example to merge multiple lists and new list created based on loop sequence number.

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:
    - name: Merge multiple lists
      set_fact:u
  	objects: "{{ objects | default([]) }} + {{ [count] }}"
      with_sequence: start=1 end=10 stride=1 format=%02d
      loop_control:
    	loop_var: count


    - name: Print the var
      debug:
        var: objects            	

Output:

$ ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [localhost] ************************************************************************************************************************************************************

TASK [Merge multiple lists] *************************************************************************************************************************************************
ok: [localhost] => (item=01)
ok: [localhost] => (item=02)
ok: [localhost] => (item=03)
ok: [localhost] => (item=04)
ok: [localhost] => (item=05)
ok: [localhost] => (item=06)
ok: [localhost] => (item=07)
ok: [localhost] => (item=08)
ok: [localhost] => (item=09)
ok: [localhost] => (item=10)

TASK [Print the var] ********************************************************************************************************************************************************
ok: [localhost] => {
	"objects": [
    	"01",
    	"02",
    	"03",
    	"04",
    	"05",
    	"06",
    	"07",
    	"08",
    	"09",
    	"10"
	]
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=2	changed=0	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0 

If we use [] outside of [{{ count }}], we will get the different output like concatenated output as string.

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:
    - name: Merge multiple lists
      set_fact:
    	objects: "{{ objects | default([]) }} + [{{ count }}]"
      with_sequence: start=1 end=10 stride=1 format=%02d
      loop_control:
    	loop_var: count


    - name: Print the var
      debug:
    	var: objects


Output:

$ ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [localhost] ************************************************************************************************************************************************************

TASK [Merge multiple lists] *************************************************************************************************************************************************
ok: [localhost] => (item=01)
ok: [localhost] => (item=02)
ok: [localhost] => (item=03)
ok: [localhost] => (item=04)
ok: [localhost] => (item=05)
ok: [localhost] => (item=06)
ok: [localhost] => (item=07)
ok: [localhost] => (item=08)
ok: [localhost] => (item=09)
ok: [localhost] => (item=10)

TASK [Print the var] ********************************************************************************************************************************************************
ok: [localhost] => {
	"objects": "[] + [01] + [02] + [03] + [04] + [05] + [06] + [07] + [08] + [09] + [10]"
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=2	changed=0	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0 

we can use below ansible code to merge the multiple dictionary list,

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:
    - name: Merge multiple lists
      set_fact:
    	objects: >-
      	{{ objects | default([]) }} + [{'name': 'object-{{ count }}'}]
      with_sequence: start=1 end=10 stride=1 format=%02d
      loop_control:
    	loop_var: count


    - name: Print the var
      debug:
    	var: objects

Output:

$ ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [localhost] ************************************************************************************************************************************************************

TASK [Merge multiple lists] *************************************************************************************************************************************************
ok: [localhost] => (item=01)
ok: [localhost] => (item=02)
ok: [localhost] => (item=03)
ok: [localhost] => (item=04)
ok: [localhost] => (item=05)
ok: [localhost] => (item=06)
ok: [localhost] => (item=07)
ok: [localhost] => (item=08)
ok: [localhost] => (item=09)
ok: [localhost] => (item=10)

TASK [Print the var] ********************************************************************************************************************************************************
ok: [localhost] => {
	"objects": [
    	{
        	"name": "object-01"
    	},
    	{
        	"name": "object-02"
    	},
    	{
        	"name": "object-03"
    	},
    	{
        	"name": "object-04"
    	},
    	{
        	"name": "object-05"
    	},
    	{
        	"name": "object-06"
    	},
    	{
        	"name": "object-07"
    	},
    	{
        	"name": "object-08"
    	},
    	{
        	"name": "object-09"
    	},
    	{
        	"name": "object-10"
    	}
	]
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=2	changed=0	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0

We can see the multiple dictionary list is created and merged as a single list.




Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^