Issue parsing array from Ansible to Shell Script

My requirement is pretty straight forward.

I wish to pass & read a set of string as array from ansible which is constructed by iterating over using with_items as show below.

  • name: set_fact

set_fact:

fpath:

set_fact:

fpath: “{{ fpath + [ BASEPATH ~ ‘/’ ~ vars[item.split(‘.’)[1]] ~ ‘/’ ~ item | basename ] }}”

with_items:

  • “{{ Source_Files.split(‘,’) }}”

vars:

fpath:

Using the set_fact module i collect the strings in a array variable called ‘fpath’ and then pass the variable to a shell script using shell module as below.

  • shell: " ~/backup.sh ‘{{ fpath }}’ "

The challenge is that the passed string array fpath is unicode and is read as below in the shell script:

[u/tmp/file.js, u/var/test.txt, u/tmp/llo.rft]

Parsing this in unix shell script becomes challenging as evident by my post to this forum and I’m looking for a better solution to my requirement.

Can you try with python3?

The servers are on python 2.7.5

I can’t use python 3, btw would like to know the solution for python 3 along with a solution for python 2.7.5 ??

Looks like no one has responded with any suggestion / solution for python 2.7.x

I would like to repost incase this is ignored.

Assuming fpath is a list or results that you want to pass as arguments
to your shell script.
Right now you are printing this list as a string, which results in:

[u/tmp/file.js, u/var/test.txt, u/tmp/llo.rft]

As you found out, that will not work.

Instead, join the items with spaces, and escape them for shell usage:

- shell: " ~/backup.sh '{{ fpath | map('quote') | join(' ') }}' "

Dick