# Python script with Json output as inventory

**URL:** <https://forum.ansible.com/t/python-script-with-json-output-as-inventory/21779>\
**Category:** Ansible Project\
**Created:** [June 14, 2016, 9:29pm UTC](https://forum.ansible.com/t/python-script-with-json-output-as-inventory/21779 "2016-06-14T21:29:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Idan\_null](https://avatars.discourse-cdn.com/v4/letter/i/eb9ed0/32.png) [@Idan\_null](https://forum.ansible.com/u/Idan_null)\
**Post date:** [June 14, 2016, 9:29pm UTC](https://forum.ansible.com/t/python-script-with-json-output-as-inventory/21779/1 "2016-06-14T21:29:00Z")

</div>

Hello to everyone 🙂  
I’m quite new to Ansible.  
I have used the Python API for ansible to connect to a few servers I have,  
I want to get the inventory list for the python script from MySQL server, so as far as I understood the list should be in Json format (this is because each host have different ports)  
But when I run the script I get a error that ansible couldn’t connect to the server:

This is the error i’m getting:

UP \*\*\*\*\*\*\*\*\*\*\*  
FAILED \*\*\*\*\*\*\*  
DOWN \*\*\*\*\*\*\*\*\*  
{  
“\_meta”: {  
“hostvars”: {  
“10.24.20.126”: {  
“ansible\_host”: “10.24.20.126” \>\>\> Failed to connect to the host via ssh.

This is the full json I’m sending to the host\_list variable in the python script  
js = {“group”: {“hosts”: [“10.24.20.126”, “10.24.20.127”], “vars”: {“ansible\_user”: “user”, “ansible\_port”: 22}}, “\_meta”: {“hostvars”: {“10.24.20.126”: {“ansible\_host”: “10.24.20.126”, “ansible\_user”: “user”, “ansible\_port”: “22”}, “10.24.20.127”: {“host\_specific\_var”: “foo”}}}}

And this is the error i’m getting:

UP \*\*\*\*\*\*\*\*\*\*\*  
FAILED \*\*\*\*\*\*\*  
DOWN \*\*\*\*\*\*\*\*\*  
{  
“\_meta”: {  
“hostvars”: {  
“10.24.20.126”: {  
“ansible\_host”: “10.24.20.126” \>\>\> Failed to connect to the host via ssh.

This ansible can connect to this server if i ping it using ansible (ansible -m ping 10.24.20.126)

10.24.20.126 | SUCCESS =\> {  
“changed”: false,  
“ping”: “pong”  
}

the python script I took from here:  
[https://github.com/ansible/ansible/blob/devel/examples/scripts/uptime.py](https://github.com/ansible/ansible/blob/devel/examples/scripts/uptime.py)

And I just send into the host\_list variable the JSON.

Any thought of why my script doesn’t work ?

Thank you evreyone

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 14, 2016, 10:37pm UTC](https://forum.ansible.com/t/python-script-with-json-output-as-inventory/21779/2 "2016-06-14T22:37:43Z")

</div>

host\_list cannot accept a JSON string. It expects either a CSV string of hosts, the path to an INI formatted file, or a path to a script that can be executed to return the JSON you are trying to pass directly to host\_list.

---

<div class="post-metadata">

**Author:** ![Idan\_null](https://avatars.discourse-cdn.com/v4/letter/i/eb9ed0/32.png) [@Idan\_null](https://forum.ansible.com/u/Idan_null)\
**Post date:** [June 15, 2016, 12:07am UTC](https://forum.ansible.com/t/python-script-with-json-output-as-inventory/21779/3 "2016-06-15T00:07:52Z")

</div>

So if my python script will run a python script that return JSON will it work ?  
I mean if i do something like that:  
$ python Ansible.py -i inv.py  
And the inv.py will return JSON  
Or it have to be  
$ ansible -m ping -i inv.py

All this hassle is because I got different servers with different ports to access to the SSH, there is no way in the host\_list in the python script to pass port for each host?
