FreeKB - Ansible uri module
Ansible - uri module

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The uri module can be used to issue a GET, PUT and POST request to a remote API. In this example, a GET request is issued to determine if http://example.com/foo.txt exists. The get_url module can be used to download files from a remote web server.

- name: determine if foo.txt exists
  uri:
    url: http://example.com/foo.txt
    method: GET
  register: out

 

If the URL is invalid, something like this will be returned.

TASK [determine if foo.txt exists]
fatal: [server1.example.com]: FAILED! => {
  "changed": false,
  "content": "",
  "elapsed": 0,
  "msg": "Status code was -1 and not [200]: Request failed: <urlopen error [Errno 111] Connection refused>",
  "redirected": false,
  "status": -1,
  "url": "http://www.example.com/foo.txt"
}

 

The failed_when parameter can be used to prevent the above from being returned when the URL is invalid. If the output being returned is to extensive, the no_log: true parameter can be used to supress the output.

- name: determine if foo.txt exists
  uri:
    url: http://example.com/foo.txt
  register: out
  failed_when: out.status != 200
  no_log: true

 

Sometimes the output produced when the file does not exist is too extensive. In this scenario, it may make sense to use the ignore_errors and no_log parameters.

- name: determine if foo.txt exists
  uri:
    url: http://example.com/foo.txt
  register: out
  ignore_errors: true
  no_log: true

- name: some custom title
  fail:
    msg: some custom error message
  when: out.failed == true

 

The debug module can the be used to print the output.

- debug: 
    var: out

 

Which should return something like this.

TASK [debug] 
ok: [server1.example.com] => {
    "var": {
        "cache_control": "no-cache, no-store", 
        "changed": false, 
        "connection": "close", 
        "content_disposition": "attachment; filename=\"foo.txt\"; filename*=UTF-8''foo.txt", 
        "content_length": "681479272",
        "content_type": "application/octet-stream", 
        "cookies": {
            "BIGipServerOC3P1-130-BITBK-EPO-T8080": "199364482.36895.0000"
        }, 
        "cookies_string": "BIGipServerOC3P1-130-AITBK-APO-T8080=799123082.36895.0000", 
        "date": "Fri, 19 Jun 2020 06:02:32 GMT", 
        "elapsed": 0, 
        "expires": "Thu, 01 Jan 1970 00:00:00 GMT", 
        "failed": false, 
        "msg": "OK (unknown bytes)", 
        "pragma": "no-cache", 
        "redirected": false, 
        "set_cookie": "BIGipServerOC3P1-130-AITBK-EPO-T8080=799399082.36895.0000; path=/; Httponly; Secure", 
        "status": 200, 
        "transfer_encoding": "chunked", 
        "url": "https://www.example.com/foo.txt", 
        "x_arequestid": "*8MTOVAx62x865730x0", 
        "x_asen": "SEN-13899879", 
        "x_auserid": "1", 
        "x_ausername": "root", 
        "x_content_type_options": "nosniff", 
        "x_download_options": "noopen", 
        "x_frame_options": "SAMEORIGIN", 
        "x_xss_protection": "1; mode=block"
    }
}

 

You probably just want the status, such as 200.

- debug: 
    var: out.status

 

If the remote system requires basic authentication, the force_basic_authurl_username and url_password options can be included.

- name: determine if foo.txt exists
  uri:
    url: http://example.com/foo.txt
    force_basic_auth: yes
    url_username: john.doe
    url_password: itsasecret
    validate_certs: no
  register: out

 

Let's say foo.txt contains "Hello World". The return_content parameter can be used.

- name: determine if foo.txt exists
  uri:
    url: http://example.com/foo.txt
    return_content: yes
  register: out

 

In this example, one of the keys in the out variable will be content.

"content": "Hello World"

 

You could then print the content.

- debug: 
    var: out.content


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter dad36 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |