1

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 ??

nauto
  • 49
  • 4
  • Hi nauto welcome to SO. I can't reproduce your error, but I'd bet it's that `| unique` is producing a `set` type in python, so adding `| list` should fold it over to a `list` type: thus: `_src: "{{ item.source | unique | list }}"` – mdaniel Nov 25 '21 at 02:49
  • @mdaniel tried your suggestion but in the debug i see :`"source": [ ".", "2","1" ]` which is not what i want , when i changed my vars like source:["1.2"] , maually added square brackets then in the debug i do see source:["1.2"]. but unfortunately my vars are dynamic some are list and some are just dict key:value so i can't manually change make them in square brackets [ ] . also its interesting that you can't reproduce on your side. my ansible version is `ansible 2.10.3` – nauto Nov 25 '21 at 05:10
  • Right, I appreciate you didn't get the end result you wanted because you're mangling the hell out of your variables, but your question was "type set is not serializable" -- did the `|list` coercion fix that error or not? – mdaniel Nov 25 '21 at 18:10

1 Answers1

1

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) }}"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • Thank you Vladimir, i am impressed the way you think and code. Wish one day i can do like you. – nauto Nov 25 '21 at 23:34