Parse JSON output from uri module

1,463 views
Skip to first unread message

Petr Sukharev

unread,
Nov 14, 2014, 11:27:33 AM11/14/14
to ansible...@googlegroups.com
Hello! I have some strange problem with register option in role and json fomat. I try to communicate with my Monitoring System (Zabbix) via Zabbix API. A need to send some data, collect some data and make some work around results.
Here is output:

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
<10.30.42.104> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO 10.30.42.104
<10.30.42.104> REMOTE_MODULE setup
<10.30.42.104> EXEC /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1415981568.28-16153856594414 && echo $HOME/.ansible/tmp/ansible-tmp-1415981568.28-16153856594414'
<10.30.42.104> PUT /tmp/tmpQ8RBGU TO /root/.ansible/tmp/ansible-tmp-1415981568.28-16153856594414/setup
<10.30.42.104> EXEC /bin/sh -c 'LC_CTYPE=en_US.UTF-8 LANG=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1415981568.28-16153856594414/setup; rm -rf /root/.ansible/tmp/ansible-tmp-1415981568.28-16153856594414/ >/dev/null 2>&1'
ok: [test-machine.example.com]
test-machine.example.com: importing /etc/ansible/vars/example-domain/example-domain
Perform task: zabbix_agent_setup | get auth data for zabbix server (y/n/c): c

Perform task: zabbix_agent_setup | get auth data for zabbix server (y/n/c): ***
<localhost> REMOTE_MODULE uri url=http://"10.20.0.83"/api_jsonrpc.php HEADER_Content-Type="application/json" method=POST body="{\"jsonrpc\":\"2.0\", \"method\":\"user.login\", \"params\":{\"user\":\"Admin\",\"password\":\"SECUREPASSWORD\"},\"id\":\"1\"}"
<localhost> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493 && echo $HOME/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493']
<localhost> PUT /tmp/tmp2H04FO TO /root/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493/uri
<localhost> EXEC ['/bin/sh', '-c', u'LC_CTYPE=en_US.UTF-8 LANG=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493/uri; rm -rf /root/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493/ >/dev/null 2>&1']
ok: [test-machine.example.com] => {"changed": false, "connection": "keep-alive", "content_type": "application/json", "date": "Fri, 14 Nov 2014 16:12:55 GMT", "json": {"id": "1", "jsonrpc": "2.0", "result": "4defb454bb5424ee06e8c7c6d1456eb6"}, "keep_alive": "timeout=1", "redirected": false, "server": "nginx", "status": 200, "transfer_encoding": "chunked", "x_powered_by": "PHP/5.3.3"}

TASK: [zabbix_agent_setup | debug var={{zabbix_auth_code.json.result}}] *******
<10.30.42.104> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO 10.30.42.104
ok: [test-machine.example.com] => {
"4defb454bb5424ee06e8c7c6d1456eb6": "{{ 4defb454bb5424ee06e8c7c6d1456eb6 }}"
}

TASK: [zabbix_agent_setup | check does host is present in zabbix] *************
<localhost> REMOTE_MODULE uri url=http://"10.20.0.83"/api_jsonrpc.php HEADER_Content-Type="application/json" method=POST body="{\"jsonrpc\":\"2.0\",\"method\":\"host.exists\",\"params\":{\"nodeids\":[\"1\",\"2\"],\"host\":\"test-machine.example.com\"},\"auth\":\"4defb454bb5424ee06e8c7c6d1456eb6\",\"id\":2}"
<localhost> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951 && echo $HOME/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951']
<localhost> PUT /tmp/tmpdXNLM0 TO /root/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951/uri
<localhost> EXEC ['/bin/sh', '-c', u'LC_CTYPE=en_US.UTF-8 LANG=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951/uri; rm -rf /root/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951/ >/dev/null 2>&1']
ok: [test-machine.example.com] => {"changed": false, "connection": "keep-alive", "content_type": "application/json", "date": "Fri, 14 Nov 2014 16:12:59 GMT", "json": {"id": 2, "jsonrpc": "2.0", "result": false}, "keep_alive": "timeout=1", "redirected": false, "server": "nginx", "status": 200, "transfer_encoding": "chunked", "x_powered_by": "PHP/5.3.3"}

TASK: [zabbix_agent_setup | debug var={{zabbix_host_exist.json.result}}] ******
<10.30.42.104> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO 10.30.42.104
ok: [test-machine.example.com] => {
"False": "False"
}

TASK: [zabbix_agent_setup | get discoved hosts group id from zabbix] **********
<localhost> REMOTE_MODULE uri url=http://"10.20.0.83"/api_jsonrpc.php HEADER_Content-Type="application/json" method=POST body="{\"jsonrpc\":\"2.0\",\"method\":\"hostgroup.getobjects\",\"params\":{\"name\":\"Discovered hosts\"},\"auth\":\"4defb454bb5424ee06e8c7c6d1456eb6\",\"id\":3}"
<localhost> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622 && echo $HOME/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622']
<localhost> PUT /tmp/tmpaiW2bl TO /root/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622/uri
<localhost> EXEC ['/bin/sh', '-c', u'LC_CTYPE=en_US.UTF-8 LANG=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622/uri; rm -rf /root/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622/ >/dev/null 2>&1']
ok: [test-machine.example.com] => {"changed": false, "connection": "keep-alive", "content_type": "application/json", "date": "Fri, 14 Nov 2014 16:13:02 GMT", "json": {"id": 3, "jsonrpc": "2.0", "result": [{"flags": "0", "groupid": "100100000000005", "internal": "1", "name": "Discovered hosts"}]}, "keep_alive": "timeout=1", "redirected": false, "server": "nginx", "status": 200, "transfer_encoding": "chunked", "x_powered_by": "PHP/5.3.3"}

TASK: [zabbix_agent_setup | debug var={{zabbix_group_id.json.result.groupid}}] ***
<10.30.42.104> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO 10.30.42.104
fatal: [test-machine.example.com] => One or more undefined variables: 'list' object has no attribute 'groupid'

FATAL: all hosts have already failed -- aborting

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
As you can see - some steps working fine. I send auth request to server ( "get auth data for zabbix server" step and register result like variable), make second request (get info about host - "check does host is present in zabbix" and register one more variable) but when i try to get one more value - some strange happened here. My request is correct and you can see response in log (get discoved hosts group id from zabbix) but i can register field "groupid". 
Plase, help me with this situation. May be i managed JSON output in wrong way?

Romeo Theriault

unread,
Nov 14, 2014, 2:53:23 PM11/14/14
to ansible...@googlegroups.com
It looks like zabbix is passing back an array containing the json data.

Try this instead: {{ zabbix_group_id.json.result[0].groupid }}

If that doesn't work, what do you get when you do: {{ zabbix_group_id.json.result[0] }} or just {{ zabbix_group_id.json.result }}

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/af5f3547-b7d2-4bb6-9b57-ea0380a95739%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Romeo

Petr Sukharev

unread,
Nov 14, 2014, 3:25:25 PM11/14/14
to ansible...@googlegroups.com
Working like charm, thanks!

i am little confused, my first and second steps get correct data, but now i need to use result[0]...

пятница, 14 ноября 2014 г., 22:53:23 UTC+3 пользователь Romeo Theriault написал:
It looks like zabbix is passing back an array containing the json data.

Try this instead: {{ zabbix_group_id.json.result[0].groupid }}

--
Romeo

Toshio Kuratomi

unread,
Nov 16, 2014, 12:24:02 PM11/16/14
to ansible...@googlegroups.com
Each module is in charge of deciding what data structures to return
and in what hierarchy so if you need the returned data from a call to
a particular piece of module functionality you do have to check that
the return value is formatted the way you expect. In this case I think
that the first two zabbix calls only return a single value but the
last function can return multiple values if there are multiple hosts
or groupids. So the zabbix module author chose to return an array in
the latter instance.

-Toshio
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/a0901598-a745-49aa-b3a2-0e49f43c13ba%40googlegroups.com.

Michael Liu

unread,
Apr 16, 2015, 10:26:45 PM4/16/15
to ansible...@googlegroups.com
Thanks this was very helpful.  Banged my head against the keyboard for about 2 hours before I found this page.
Reply all
Reply to author
Forward
0 new messages