Parse JSON output from uri module

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): ***
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"}”
EXEC [‘/bin/sh’, ‘-c’, ‘mkdir -p $HOME/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493 && echo $HOME/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493’]
PUT /tmp/tmp2H04FO TO /root/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493/uri
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] *************
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}”
EXEC [‘/bin/sh’, ‘-c’, ‘mkdir -p $HOME/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951 && echo $HOME/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951’]
PUT /tmp/tmpdXNLM0 TO /root/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951/uri
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] **********
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}”
EXEC [‘/bin/sh’, ‘-c’, ‘mkdir -p $HOME/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622 && echo $HOME/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622’]
PUT /tmp/tmpaiW2bl TO /root/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622/uri
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

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

Working like charm, thanks!

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

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

Thanks this was very helpful. Banged my head against the keyboard for about 2 hours before I found this page.