Hello, I need help in with getting my play-book to parse data that is coming back from Ansible URI Response using "ansible.builtin.uri" module, trying to parse data json saved as var=result. But I keep seeing the following message, saying variable is not defined ! 'dict object has no attribute 'json'. The play-book come back with no syntax error nor any failures just this message below.
Message: "result.json.data": "VARIABLE IS NOT DEFINED!: 'dict object' has no attribute 'json'" see below: $ cat uricall.yml --- - name: Validate KIE Server deployment hosts: all become: false gather_facts: false vars: ansible_python_interpreter: /opt/bin/python kie_server_server: "http://reqres.in/api/users?page=2" kie_server_endpoint: "/kie-server/services/rest/server/containers" kie_server_port: ":13421" kieserver_user: "user" kieserver_password: "somepassword" expected_server_version: "7.72.0.Final" tasks: - name: list details ansible.builtin.uri: url: "{{ kie_server_server }}{{ kie_server_port }}{{ kie_server_endpoint }}" user: "{{ kieserver_user }}" password: "{{ kieserver_password }}" method: GET validate_certs: false status_code: 200 timeout: 30 register: result - name: debug ansible.builtin.debug: var: result.json.data // ------------------------------------------------------------------------ $ python3 --version Python 3.6.8 $ ansible --version ansible [core 2.13.3] config file = /users/admin/ansible.cfg configured module search path = ['/users/admin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3.9/site-packages/ansible ansible collection location = /users/admin/.ansible/collections:/usr/share/ansible/collections executable location = /usr/bin/ansible python version = 3.9.13 (main, Nov 9 2022, 13:16:24) [GCC 8.5.0 20210514 (Red Hat 8.5.0-15)] jinja version = 3.1.2 libyaml = True $ // ------------------------------------------------------------------------ $ ansible-playbook uricall.yml -l reqres.in/api/users?page=2 -vvv ansible-playbook [core 2.13.3] config file = /users/admin/ansible.cfg configured module search path = ['/users/admin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3.9/site-packages/ansible ansible collection location = /users/admin/.ansible/collections:/usr/share/ansible/collections executable location = /usr/bin/ansible-playbook python version = 3.9.13 (main, Nov 9 2022, 13:16:24) [GCC 8.5.0 20210514 (Red Hat 8.5.0-15)] jinja version = 3.1.2 libyaml = True Using /users/admin/ansible.cfg as config file host_list declined parsing /users/admin/hosts as it did not pass its verify_file() method script declined parsing /users/admin/hosts as it did not pass its verify_file() method auto declined parsing /users/admin/hosts as it did not pass its verify_file() method Parsed /users/admin/hosts inventory source with ini plugin Skipping callback 'default', as we already have a stdout callback. Skipping callback 'minimal', as we already have a stdout callback. Skipping callback 'oneline', as we already have a stdout callback. PLAYBOOK: uricall.yml ************************************************************************************************************************************************* 1 plays in uricall.yml PLAY [Validate KIE Server deployment] ********************************************************************************************************************************* META: ran handlers TASK [list details] *************************************************************************************************************************************************** task path: /users/admin/uricall.yml:15 <reqres.in/api/users?page=2> ESTABLISH SSH CONNECTION FOR USER: None <reqres.in/api/users?page=2> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/users/admin/.ansible/cp/2abdb2e15a"' reqres.in/api/users?page=2 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"'' <reqres.in/api/users?page=2> (0, b'/users/admin\n', b'') <reqres.in/api/users?page=2> ESTABLISH SSH CONNECTION FOR USER: None <reqres.in/api/users?page=2> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/users/admin/.ansible/cp/2abdb2e15a"' reqres.in/api/users?page=2 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /users/admin/.ansible/tmp `"&& mkdir "` echo /users/admin/.ansible/tmp/ansible-tmp-1676942607.0676842-2055362-84558986433951 `" && echo ansible-tmp-1676942607.0676842-2055362-84558986433951="` echo /users/admin/.ansible/tmp/ansible-tmp-1676942607.0676842-2055362-84558986433951 `" ) && sleep 0'"'"'' <reqres.in/api/users?page=2> (0, b'ansible-tmp-1676942607.0676842-2055362-84558986433951=/users/admin/.ansible/tmp/ansible-tmp-1676942607.0676842-2055362-84558986433951\n', b'') Using module file /usr/lib/python3.9/site-packages/ansible/modules/uri.py <reqres.in/api/users?page=2> PUT /users/admin/.ansible/tmp/ansible-local-2055349bkonkdfj/tmpy1xvsbm3 TO /users/admin/.ansible/tmp/ansible-tmp-1676942607.0676842-2055362-84558986433951/AnsiballZ_uri.py <reqres.in/api/users?page=2> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/users/admin/.ansible/cp/2abdb2e15a"' '[reqres.in/api/users?page=2]' <reqres.in/api/users?page=2> (0, b'sftp> put /users/admin/.ansible/tmp/ansible-local-2055349bkonkdfj/tmpy1xvsbm3 /users/admin/.ansible/tmp/ansible-tmp-1676942607.0676842-2055362-84558986433951/AnsiballZ_uri.py\n', b'') <reqres.in/api/users?page=2> ESTABLISH SSH CONNECTION FOR USER: None <reqres.in/api/users?page=2> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/users/admin/.ansible/cp/2abdb2e15a"' reqres.in/api/users?page=2 '/bin/sh -c '"'"'chmod u+x /users/admin/.ansible/tmp/ansible-tmp-1676942607.0676842-2055362-84558986433951/ /users/admin/.ansible/tmp/ansible-tmp-1676942607.0676842-2055362-84558986433951/AnsiballZ_uri.py && sleep 0'"'"'' <reqres.in/api/users?page=2> (0, b'', b'') <reqres.in/api/users?page=2> ESTABLISH SSH CONNECTION FOR USER: None <reqres.in/api/users?page=2> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/users/admin/.ansible/cp/2abdb2e15a"' -tt reqres.in/api/users?page=2 '/bin/sh -c '"'"'/opt/bin/python /users/admin/.ansible/tmp/ansible-tmp-1676942607.0676842-2055362-84558986433951/AnsiballZ_uri.py && sleep 0'"'"'' <reqres.in/api/users?page=2> (0, b'\r\n{"status": 200, "content_length": "3529", "cookies": {"JSESSIONID": "LLNxkYsDklms77MV4ofo8r1d6yUCuu-Krc4iR_aaz5jD3_EDIl8c!1154788022"}, "date": "Tue, 21 Feb 2023 01:23:29 GMT", "set_cookie": "JSESSIONID=LLNxkYsDklms77MV4ofo8r1d6yUCuu-Krc4iR_aaz5jD3_EDIl8c!1154788022; path=/; HttpOnly", "url": "http://reqres.in/api/users?page=2:13421/kie-server/services/rest/server/containers", "changed": false, "elapsed": 0, "invocation": {"module_args": {"force": false, "remote_src": false, "status_code": [200], "owner": null, "body_format": "raw", "client_key": null, "group": null, "use_gssapi": false, "unix_socket": null, "unsafe_writes": false, "serole": null, "setype": null, "follow_redirects": "safe", "unredirected_headers": [], "return_content": false, "method": "GET", "mode": null, "body": null, "timeout": 30, "src": null, "dest": null, "selevel": null, "force_basic_auth": false, "removes": null, "http_agent": "ansible-httpget", "user": "user", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "use_proxy": true, "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "url": "http://reqres.in/api/users?page=2:13421/kie-server/services/rest/server/containers", "seuser": null, "client_cert": null, "creates": null, "headers": {}, "ca_path": null, "url_username": "user", "attributes": null, "validate_certs": false}}, "connection": "close", "content_type": "application/xml", "msg": "OK (3529 bytes)", "redirected": false, "cookies_string": "JSESSIONID=LLNxkYsDklms77MV4ofo8r1d6yUCuu-Krc4iR_aaz5jD3_EDIl8c!1154788022"}\r\n', b'Shared connection to reqres.in/api/users?page=2 closed.\r\n') <reqres.in/api/users?page=2> ESTABLISH SSH CONNECTION FOR USER: None <reqres.in/api/users?page=2> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath="/users/admin/.ansible/cp/2abdb2e15a"' reqres.in/api/users?page=2 '/bin/sh -c '"'"'rm -f -r /users/admin/.ansible/tmp/ansible-tmp-1676942607.0676842-2055362-84558986433951/ > /dev/null 2>&1 && sleep 0'"'"'' <reqres.in/api/users?page=2> (0, b'', b'') ok: [reqres.in/api/users?page=2] => { "changed": false, "connection": "close", "content_length": "3529", "content_type": "application/xml", "cookies": { "JSESSIONID": "LLNxkYsDklms77MV4ofo8r1agawergad6yUCuu-Krc4iR_aaz5jD3_EDIl8c!1154788022" }, "cookies_string": "JSESSIONID=LLNxkYsgswDklms77MV4ofqo8r1hd6yUCuu-Krc4iR_aaaz5jD3_EDIl8c!1154788022", "date": "Tue, 21 Feb 2023 01:23:29 GMT", "elapsed": 0, "invocation": { "module_args": { "attributes": null, "body": null, "body_format": "raw", "ca_path": null, "client_cert": null, "client_key": null, "creates": null, "dest": null, "follow_redirects": "safe", "force": false, "force_basic_auth": false, "group": null, "headers": {}, "http_agent": "ansible-httpget", "method": "GET", "mode": null, "owner": null, "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "remote_src": false, "removes": null, "return_content": false, "selevel": null, "serole": null, "setype": null, "seuser": null, "src": null, "status_code": [ 200 ], "timeout": 30, "unix_socket": null, "unredirected_headers": [], "unsafe_writes": false, "url": "http://reqres.in/api/users?page=2:13421/kie-server/services/rest/server/containers", "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "url_username": "user", "use_gssapi": false, "use_proxy": true, "user": "user", "validate_certs": false } }, "msg": "OK (3529 bytes)", "redirected": false, "set_cookie": "JSESSIONID=LLNxkYsDklms77MV4ofo8r1d6yUCuu-Krc4iR_aaz5jD3_EDIl8c!1154788022; path=/; HttpOnly", "status": 200, "url": "http://reqres.in/api/users?page=2:13421/kie-server/services/rest/server/containers" } TASK [debug] ********************************************************************************************************************************************************** task path: /users/admin/uricall.yml:26 ok: [reqres.in/api/users?page=2] => { "result.json.data": "VARIABLE IS NOT DEFINED!: 'dict object' has no attribute 'json'" } META: ran handlers META: ran handlers PLAY RECAP ************************************************************************************************************************************************************ reqres.in/api/users?page=2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 On Saturday, February 18, 2023 at 2:10:37 PM UTC-8 Sohail Jaffer wrote: > *Hello,* > * I am new to this group and ansible as well. I would like to > skill up and be able to perform config management tasks using Ansible > playbooks. The current problem I am working on is to be able to validate > KIE server deployment. I made little progress by checking your group and > other blogs but stuck with this error below. Please help if you can > identify the issue right away or if you could guide me where to look for > validating rest api's using ansible. * > > *// ------------------------------ ERROR MESSAGE > -----------------------------------* > > ******************** > fatal: [localhost]: FAILED! => {"msg": "The conditional check > 'results.content is search(\"SUCCESS\")' failed. The error was: error while > evaluating conditional (results.content is search(\"SUCCESS\")): 'dict > object' has no attribute 'content'"} > > // --------------------- YAML ------------------------------- > > > > > > > > > > > > > > > > > > > > > > > > > > > *- name: Validate KIE Server deployment hosts: localhost vars: > kie_server_host: "https://apiurl.com <https://apiurl.com>" > kie_server_port: "13001" kieserver_user: "user" kieserver_password: > "somepassword" expected_server_version: "7.72.0.Final" tasks: - > name: Get KIE Server information uri: url: "http://{{ > kie_server_host }}:{{ kie_server_port }}/kie-server/services/rest/server/" > user: "{{ kieserver_user }}" password: "{{ kieserver_password > }}" return_content: yes method: GET register: results > - name: Check server version assert: that: results.content is > search("SUCCESS") - debug: var: results* > > * Here it goes:* > > *When I do a dry run of the playbook I see following results --- saying > that ansible.legacy.uri does not support check mode* > > $ ansible-playbook newapicall.yml -C > [WARNING]: provided hosts list is empty, only localhost is available. Note > that the implicit localhost does not match 'all' > > PLAY [Validate KIE Server deployment] > ***************************************************************************************************** > > TASK [Gathering Facts] > ******************************************************************************************************************** > ok: [localhost] > > TASK [Get KIE Server information] > ********************************************************************************************************* > skipping: [localhost] > > TASK [debug] > ****************************************************************************************************************************** > ok: [localhost] => { > "results": { > "changed": false, > "failed": false, > "msg": "remote module (ansible.legacy.uri) does not support check > mode", > "skipped": true > } > } > > PLAY RECAP > ******************************************************************************************************************************** > localhost : ok=2 changed=0 unreachable=0 > failed=0 skipped=1 rescued=0 ignored=0 > > > *But when I finally run the play book - Debug of VAR = results returns > content from KIE server that I need to use to parse information like if > "SUCCESS" is seen in content then deployment was successful e.g.* > > > > > > *// ------------------ check ---------------------- - name: Check > server version assert: that: results.content is > search("SUCCESS")// -----------------------------------------------* > > > > $ ansible-playbook newapicall.yml > [WARNING]: provided hosts list is empty, only localhost is available. Note > that the implicit localhost does not match 'all' > > PLAY [Validate KIE Server deployment] > ***************************************************************************************************** > > TASK [Gathering Facts] > ******************************************************************************************************************** > ok: [localhost] > > TASK [Get KIE Server information] > ********************************************************************************************************* > ok: [localhost] > > TASK [debug] > ****************************************************************************************************************************** > ok: [localhost] => { > "results": { > "changed": false, > "connection": "close", > "content": "<?xml version=\"1.0\" encoding=\"UTF-8\" > standalone=\"yes\"?>\n<response type=\"SUCCESS\" msg=\"Kie Server info\">\n > <kie-server-info>\n <capabilities>KieServer</capabilities>\n > <capabilities>BRM</capabilities>\n > <capabilities>BPM</capabilities>\n > <capabilities>CaseMgmt</capabilities>\n > <capabilities>BPM-UI</capabilities>\n > <capabilities>BRP</capabilities>\n > <capabilities>DMN</capabilities>\n > <capabilities>Swagger</capabilities>\n <location> > https://apiurl.com:11001/kie-server/services/rest/server</location>\n > <messages>\n <content>Server > KieServerInfo{serverId='kie-weblogic-qa1', version='7.72.0.Final', > name='kie-weblogic-qa1', location=' > https://apiurl.com:11001/kie-server/services/rest/server', > capabilities=[KieServer, BRM, BPM, CaseMgmt, BPM-UI, BRP, DMN, Swagger]', > messages=null', mode=DEVELOPMENT}started successfully at Tue Jan 24 > 20:56:01 UTC 2023</content>\n <severity>INFO</severity>\n > <timestamp>2023-01-24T20:56:01.597Z</timestamp>\n </messages>\n > <mode>DEVELOPMENT</mode>\n <name>kie-weblogic-qa1</name>\n > <id>kie-weblogic-qa1</id>\n <version>7.72.0.Final</version>\n > </kie-server-info>\n</response>\n", > "content_length": "1252", > "content_type": "application/xml", > "cookies": { > "JSESSIONID": > "xHRmZgYJkoG2J89BjT66_01TEr_-Zb0wJx1sPnfsxTH8lCz1Tzyv!1154788022" > }, > "cookies_string": > "JSESSIONID=xHRmZgYJkoG2J89BjT66_01TEr_-Zb0wJr1SPnfsxTH8lwcz1Tyv!1154788022", > "date": "Sat, 18 Feb 2023 21:20:07 GMT", > "elapsed": 0, > "failed": false, > "msg": "OK (1252 bytes)", > "redirected": false, > "set_cookie": > "JSESSIONID=xHRmZgqG2J89BjT66_01TEr_-Zb0wJx1rSPnfsxTH8lwCz1Tyv!1154788022; > path=/; HttpOnly", > "status": 200, > "url": "https://apiurl.com:11001/kie-server/services/rest/server/" > } > } > > PLAY RECAP > ******************************************************************************************************************************** > localhost : ok=3 changed=0 unreachable=0 > failed=0 skipped=0 rescued=0 ignored=0 > > > playbook: newapicall.yml > $ ansible-playbook newapicall.yml -C > [WARNING]: provided hosts list is empty, only localhost is available. Note > that the implicit localhost does not match 'all' > > PLAY [Validate KIE Server deployment] > ***************************************************************************************************** > > TASK [Gathering Facts] > ******************************************************************************************************************** > ok: [localhost] > > TASK [Get KIE Server information] > ********************************************************************************************************* > skipping: [localhost] > > TASK [Check server vresion] > *************************************************************************************************************** > fatal: [localhost]: FAILED! => {"msg": "The conditional check > 'results.content is search(\"SUCCESS\")' failed. The error was: error while > evaluating conditional (results.content is search(\"SUCCESS\")): 'dict > object' has no attribute 'content'"} > > PLAY RECAP > ******************************************************************************************************************************** > localhost : ok=1 changed=0 unreachable=0 > failed=1 skipped=1 rescued=0 ignored=0 > > > *Below is the full playbook with some information removed for security. > But wen I check for server version or if check for content showing > "SUCCESS" when "status": 200 or even if the kie server deployment was > successful with given containers would be useful information.* > > > > > > > > > > > *e.g.# that:# - results.status == 200# - > "results.json.version" == "{{ expected_server_version }}"e.g.when kie > server returns containre information to be parsed"content": "<?xml > version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<response > type=\"SUCCESS\" msg=\"List of created containers\">\n > <kie-containers>\n * > > > > Thank you in advance -- I have put my comments in italics and code in BOLD > to separate the output from debug and my comments. I think I may have > mixed up in content format expectations, it is returning in xml and code > expects json. ?? or may be more issues. > > Regards > Sohail Jaffer > -- 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-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d75fb02e-2822-4ff1-a44f-21c7807f4a0bn%40googlegroups.com.