I have 2 scripts a.py and b.py.
I have an ansible playbook which runs a.py and b.py. Here is the script
Output of a.py is given to b.py. The problem is the Ouput of a.py is a list which can be indexed but whenever I pass the yaml's stdout to b.py as command line argument it takes as str object and it can be indexed as before i.e like a.py's.
script a.py
a = [{
"name": "jack",
"date": "Jan-06-2021",
"age": "24",
},
{
"name": "jack1",
"date": "Jan-07-2021",
"age": "25",
},
]
print(a)
b.py script
import sys
import json
random_variable = sys.argv[1]
print(json.loads(random_variable)) # tried this no use
I want to parse the values of a just like random_variab.e[0] and random_variable[1].
random_variable[0] could be like:
{'name': 'jack', 'date': 'Jan-06-2021', 'age': '24'}
- name: testing
gather_facts: True
hosts: localhost
tasks:
- name: run python script
command: python3 /test-repo/test_python/a.py
register: result
- set_fact:
var1: "{{result.stdout}}"
- debug:
msg: "{{var1}}"
- name: run python script2
command: python3 /test-repo/test_python/b.py {{var1}}
register: result
- debug:
msg: "{{result}}"
Error I am getting now
["Traceback (most recent call last):", " File \"/test-repo/test_python/b.py\", line 4, in <module>", " print(json.loads(a))", " File \"/usr/lib64/python3.6/json/__init__.py\", line 354, in loads", " return _default_decoder.decode(s)", "
File \"/usr/lib64/python3.6/json/decoder.py\", line 339, in decode", " obj, end = self.raw_decode(s, idx=_w(s, 0).end())", " File
\"/usr/lib64/python3.6/json/decoder.py\", line 355, in raw_decode", " obj, end = self.scan_once(s, idx)", "json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)"]
Expected output when I print
random_variable = sys.argv[1]
print(random_variable[0])
Output I need
{'name': 'jack', 'date': 'Jan-06-2021', 'age': '24'}