How to read and print Ansible set_fact array in unix shell script

2019-08-08T14:20:37

I created a String array using set_fact which contains path to different files as below.

   - name: set_fact
     set_fact
       fpath: []
     set_fact:
       fpath: "{{ fpath + [ BASEPATH ~ '/' ~ item|basename ] }}"
     loop: "{{ Source_Files.split(',') }}"
     vars:
      fpath: []

   - name: Printing fpath
     debug:
       var: fpath

I pass the variable fpath to a shell script as below:

   - shell: "~/backup.sh '{{ fpath }}' > ~/backup.log"

Below is my backup.sh

echo "Prameter 1:"$1 > ~/param.out

IFS=
FPATH=`python <<< "print ' '.join($FPATH)"`
echo "${FPATH[@]}"
for a in "$FPATH"; do
  echo "Printing Array: $a"
  echo $a >> ~/stuffarray.txt
done

Prameter 1 print all the three files in the below format

Output:

Prameter 1:[u/tmp/scripts/file1.src, u/var/logs/wow.txt, u/tmp.hello.exe]

However the conversion of $1 which is a python string array to a Unix shell script array is not happening and it does not print any value for Unix shell script array. This is more of a passing python style string array and reading it in the shell script by looking through the values.

I'm on the latest version of ansible and python.

Can you please suggest how can I pass the the python string array from ansible to a shell script variable so I could loop through it ?

Copyright License:
Author:「mohtashim」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/57406288/how-to-read-and-print-ansible-set-fact-array-in-unix-shell-script

About “How to read and print Ansible set_fact array in unix shell script” questions

I created a String array using set_fact which contains path to different files as below. - name: set_fact set_fact fpath: [] set_fact: fpath: "{{ fpath + [ BASEPATH ~ '...
I have a playbook that first creates a virtual machine on a vSphere cluster. It uses hosts: localhost so the API is called by the ansible-runner. Creation of VM works fine. After the VM is created ...
I need to get a shell output to a variable in set_fact in ansible. And this variable is referred in the somewhere in the same playbook but targeting a different set of hosts But I am not sure how t...
I have a ansible playbook that has set a json variable using set_fact module. I'm trying to use jq command in ansible playbook, to transform that json to a particular format, but got parse error: I...
I am trying to dynamically build the inventory of an "elasticsearch" cluster using the add_hosts module. I have these tasks: In the first task (Identify ES nodes in cluster), I query the elastic...
I am having some troubles to call some ansible in a shell script. I declare my variables in the shell script calling my ansible variables like this: var_shell= {{ var1_ansible }}{{ var2_ansible}} ...
I'm looking for advice. I have the following code that creates a list dynamically that I can then later use in a template. This is a copy of the test code I put together - for the actual role I j...
I have a text file in HDFS location, i want to read the file using unix shell script (bash profile).
I am using ansible 2.0.1 on RHEL 7 hosts. Somehow following roles when called through playbook skips the Get the yellow major version. Attaching output below. Looks like there is something wrong w...
I m very new to ansible and facing some difficulty in this I have to pass the full shell command based on some condition in ansible I tried to set facts but i'm not able to execute the shell and ge...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.