Unable to union another list to a list variable

I am trying to have a condition on a single item of a ‘with_items’ list but as i cannot do it directly, i found the following snippet:

  • name: prepare a list of common files that must be copied
    set_fact: commonFiles=[‘1/f1’, ‘1/f2’]

  • copy: src=“{{item}}” dest=2
    with_items: “{{ commonFiles | union ( (1 == 2) | ternary ( [‘1/f3’], )) }}”

This gives me following error:

fatal: [this]: FAILED! => {“failed”: true, “msg”: “Unexpected templating type error occurred on ({{ commonFiles | union ( (1 == 2) | ternary ( [‘1/f3’], )) }}): coercing to Unicode: need string or buffer, list found”}

I am not sure why using a var of list types gives me an error, while the following works fine:

  • copy: src=“{{item}}” dest=2
    with_items: “{{ [‘1/f1’, ‘1/f2’] | union ( (1 == 2) | ternary ( [‘1/f3’], )) }}”

commonFiles will be a string when using Ansible key value format and the value contain a space.

I recommend using the YAML format.

- name: prepare a list of common files that must be copied
   set_fact:
     commonFiles: ['1/f1', '1/f2']

Thanks, That worked fine. Although i still don’t understand what just happened. You say that earlier it was taking commonFiles as string bu the error message says ‘expecting string found list’.