# Issue parsing array from Ansible to Shell Script

**URL:** <https://forum.ansible.com/t/issue-parsing-array-from-ansible-to-shell-script/31000>\
**Category:** Ansible Project\
**Created:** [August 9, 2019, 6:54am UTC](https://forum.ansible.com/t/issue-parsing-array-from-ansible-to-shell-script/31000 "2019-08-09T06:54:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 9, 2019, 6:54am UTC](https://forum.ansible.com/t/issue-parsing-array-from-ansible-to-shell-script/31000/1 "2019-08-09T06:54:26Z")

</div>

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](https://www.unix.com/shell-programming-and-scripting/282469-unable-print-python-array-shell-script-loop.html) and I’m looking for a better solution to my requirement.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 9, 2019, 10:15am UTC](https://forum.ansible.com/t/issue-parsing-array-from-ansible-to-shell-script/31000/2 "2019-08-09T10:15:19Z")

</div>

Can you try with python3?

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 9, 2019, 10:21am UTC](https://forum.ansible.com/t/issue-parsing-array-from-ansible-to-shell-script/31000/3 "2019-08-09T10:21:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 12, 2019, 8:17pm UTC](https://forum.ansible.com/t/issue-parsing-array-from-ansible-to-shell-script/31000/4 "2019-08-12T20:17:36Z")

</div>

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

I would like to repost incase this is ignored.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 13, 2019, 10:53am UTC](https://forum.ansible.com/t/issue-parsing-array-from-ansible-to-shell-script/31000/5 "2019-08-13T10:53:47Z")

</div>

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
