Unable to union another list to a list variable

28 views
Skip to first unread message

ishan jain

unread,
Mar 13, 2017, 2:08:37 PM3/13/17
to Ansible Project
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'], [])) }}"

Kai Stian Olstad

unread,
Mar 13, 2017, 3:18:23 PM3/13/17
to ansible...@googlegroups.com
On 13. mars 2017 19:08, ishan jain wrote:
> 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']

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']

--
Kai Stian Olstad

ishan jain

unread,
Mar 14, 2017, 6:08:21 AM3/14/17
to Ansible Project, ansible-pr...@olstad.com
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'.
Reply all
Reply to author
Forward
0 new messages