Ansible Tutorial
uri ansibke module is available to test any webservice URL's.
Interacts with HTTP and HTTPS web services and supports Digest, Basic and WSSE HTTP authentication mechanisms.
url is mandatory parameter and need to provide HTTP or HTTPS URL in the form '[http|https]://webserver.domain[:port]/path'
method is not a mandatory parameter for uri module, it is the HTTP method of the request or response and MUST be uppercase.
method default value is GET and possible values are,
GET POST PUT HEAD DELETE OPTIONS PATCH TRACE CONNECT REFRESH
return_content parameter is not required and default value no, return_content is used to specify whether or not to return the body of the response as a "content" key in the dictionary result.
timeout parameter is socket level timeout in seconds and default is 30 seconds.
Let's see the example ansible playbook to get url status,
--- - name: Test URL status hosts: localhost tasks: - name: Set url details set_fact: test_url: "https://codingpointer.com" - name: "check the status of the remote server from localhost" uri: url: "{{ test_url }}" method: GET timeout: 30 return_content: yes register: out - name: Print the msg debug: msg: "{{ out }}"
This ansible playbook is using uri module with GET method to test the status of url 'https://codingpointer.com'.
Output:
$ ansible-playbook test-url.yml PLAY [Test URL status] ****************************************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************************************************** ok: [localhost] TASK [Set url details] ****************************************************************************************************************************************************** ok: [localhost] TASK [check the status of the remote server from localhost] ***************************************************************************************************************** ok: [localhost] TASK [Print the msg] ******************************************************************************************************************************************************** ok: [localhost] => { "msg": { "changed": false, "connection": "Upgrade, close", "content": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <title>Programming tutorial with examples</title>\n </head>\n<body> ---------------webpage content ..................</body>\n</html>", "content_type": "text/html; charset=UTF-8", "cookies": {}, "cookies_string": "", "date": "Thu, 18 Nov 2021 18:11:53 GMT", "elapsed": 1, "failed": false, "msg": "OK (unknown bytes)", "redirected": false, "server": "Apache", "status": 200, "transfer_encoding": "chunked", "upgrade": "h2,h2c", "url": "https://codingpointer.com", "vary": "Accept-Encoding", "x_powered_by": "PHP/7.1.33" } } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
This uri module returns output in different format than usual and displayed the result of uri module ansibke task here.
Used valid url and return_content is yes, so webpage content is displayed here.
Let's use invalid URL in above ansible playbook and see the output,
--- - name: Test URL status hosts: localhost tasks: - name: Set url details set_fact: test_url: "https://codingpointer1.com" - name: "check the status of the remote server from localhost" uri: url: "{{ test_url }}" method: GET timeout: 30 return_content: yes register: out - name: Print the msg debug: msg: "{{ out }}"
Output:
$ ansible-playbook test-url.yml PLAY [Test URL status] ****************************************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************************************************** ok: [localhost] TASK [Set url details] ****************************************************************************************************************************************************** ok: [localhost] TASK [check the status of the remote server from localhost] ***************************************************************************************************************** fatal: [localhost]: FAILED! => {"changed": false, "content": "", "elapsed": 0, "msg": "Status code was -1 and not [200]: Request failed:", "redirected": false, "status": -1, "url": "https://codingpointer1.com"} PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Ansible Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page