Bootstrap FreeKB - Ansible - Determine if a remote file or directory exists using the uri module
Ansible - Determine if a remote file or directory exists using the uri module

Updated:   |  Ansible articles

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

The uri module can be used to determine if a web based file exists. In this example, the uri module is used to determine if http://example.com/foo.txt exists. The get_url module can be used to download files from a remote web server.

Notice that hosts: localhost is used, meaning the tasks is run on the Ansible control, not on the managed nodes. This task can be run on either the Ansible control or managed nodes, as long as the Ansible control or managed nodes are able to make calls out to remote systems.

---
- hosts: localhost
  tasks:
  - name: determine if foo.txt exists
    uri:
      url: http://example.com/foo.txt
    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



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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