Bootstrap FreeKB - Ansible - Download files from a remote system using get_url
Ansible - Download files from a remote system using get_url

Updated:   |  Ansible articles

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

The get_url module can be used to download files from a remote system, which is similar to the wget command on Linux. The uri module can be used to determine if files on a remote web server exist. The debug module can then be used to print the output.

---
- hosts: localhost
  tasks:
  - name: download foo.txt
    get_url:
      url: http://www.example.com/foo.txt
      dest: /tmp/foo.txt
    register: out

  - debug: 
      var: out
...

 

If the source file does not exist, the following will be returned.

TASK [download foo.txt]
ok: [server1.example.com]: FAILED! => {
  "changed": false,
  "dest": "/tmp/foo.txt",
  "elapsed": 0,
  "msg": "Request failed",
  "response": "HTTP Error 404: Not Found",
  "status_code": 404,
  "url": "http://www.example.com/foo.txt" }

 

If the source file exists, something like this should be returned.

TASK [output the content of the 'out' variable] 
ok: [server1.example.com] => {
    "msg": {
        "changed": true, 
        "checksum_dest": null, 
        "checksum_src": "977780a4470e1149a968c04a7c463f04c7c4a9dc", 
        "dest": "/tmp/foo.txt", 
        "elapsed": 0, 
        "failed": false, 
        "gid": 0, 
        "group": "root", 
        "md5sum": "ab14907c4e32ab8797f3afa54fb5cab4", 
        "mode": "0755", 
        "msg": "OK (unknown bytes)", 
        "owner": "root", 
        "secontext": "system_u:object_r:bin_t:s0", 
        "size": 41639736, 
        "src": "/root/.ansible/tmp/ansible-tmp-1592560655.86-151504298398038/tmplN_yXZ", 
        "state": "file", 
        "status_code": 200, 
        "uid": 0, 
        "url": "https://example.com/foo.txt"
    }
}

 

You probably just want the status, such as 200.

- debug: 
    var: out.status

 

By default, "force" will be set to false, meaning the file will not be downloads if the file on the target system matches the file being downloaded. force:true can be used to always download the file.

AVOID TROUBLE

If the destination file already exists, the destination file will be overwritten, even if you use the force: no parameter, due to the bug described here: https://github.com/ansible/ansible/issues/64016

---
- hosts: localhost
  tasks:
  - name: download foo.txt
    get_url:
      url: http://www.example.com/foo.txt
      dest: /tmp/foo.txt
      force: true
    register: out
...

 

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

---
- hosts: localhost
  tasks:
  - name: download foo.txt
    get_url:
      url: http://example.com/foo.txt
      dest: /tmp/foo.txt
      force_basic_auth: yes
      url_username: john.doe
      url_password: itsasecret
      validate_certs: no
...

 




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 7779b6 in the box below so that we can be sure you are a human.