My scenario is a bit similar to the one mentioned here
but a bit difference, let me take same example mentioned in above question solution by Vladimir Botka , added name:c and name:d block
output:
- Name: A
source: [a, b, c, a, b, c]
dest: [x,y,z]
- Name: B
source: [a, b, c, a, b, c]
dest: [x.c, y, z, x, y, z]
- Name: C
source:
dest:
- Name: D
source: "1.2"
dest: "x"
I am using same answer suggested in above post but a bit modifications as :
- set_fact:
out1: "{{output |replace('None','[]')}}"
- set_fact:
out: "{{ out|d([]) [{'Name': item.Name,
'source': _src,
'dest': _dst}] }}"
loop: "{{ out1 }}"
vars:
_src: "{{ item.source|unique}}"
_dst: "{{ item.dest|unique}}"
- debug:
var: out
I used out1: "{{output |replace('None','[]')}}"
cause loop is failing for name:c
as source and dest is empty and not a list i am making it list by replacing None
as []
, to over come loop error, i am able to fix loop error.
not sure if its right approach or not but solved loop error issue.
but second error i am getting is Object of type set is not JSON serializable
for name:d
cause source and destination is not a list.
how can this error be fixed or any workaround ??
CodePudding user response:
Q: "Error when source and destination is not a list"
A: Filter unique items if the type of the value is a list otherwise copy the value, e.g.
- set_fact:
out: "{{ out|d([]) [{'Name': item.Name,
'source': _src,
'dest': _dst}] }}"
loop: "{{ out1 }}"
vars:
_src: "{{ (item.source|type_debug == 'list')|
ternary(item.source|unique, item.source) }}"
_dst: "{{ (item.dest|type_debug == 'list')|
ternary(item.dest|unique, item.dest) }}"