FreeKB - Ansible get_url module
Ansible - get_url module

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. The uri module can be used to determine if files on a remote web server exist.

- name: download foo.txt
  get_url:
    url: http://www.example.com/foo.txt
    dest: /tmp/foo.txt
  register: 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" }

 

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

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

- name: output the content of the 'out' variable
  debug: 
    var: out

 

Which should return something like this.

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

 

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

- 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"

 


Optional Parameters

The get_url module accepts the following parameters.

  • force_basic_auth - yes or no
  • owner (e.g. owner: "root")
  • group (e.g. group: "wheel")
  • mode (e.g. mode: "2775")
  • seuser - SELinux user (e.g. seuser: "unconfined_u")
  • serole - SELinux role (e.g. serole: "object_r")
  • setype - SELinux type (e.g. setype: "httpd_sys_content_t")
  • selevel - SELinux level (e.g. selevel: "s0")
  • url_username - (e.g. john.doe)
  • url_password - (e.g. itsasecret)
  • validate_certs: yes or no

 

Here is an example of how these parameters would be used.

- name: "download foo.txt"
  get_url:
    url: "http://example.com/foo.txt"
    dest: "/tmp/foo.txt"
    force_basic_auth: "yes"
    owner: "foo"
    group: "foo"
    mode: "0644"
    seuser: "unconfined_u"
    serole: "object_r"
    setype: "httpd_sys_content_r"
    selevel: "s0"
    url_username: "john.doe"
    url_password: "itsasecret"
    validate_certs: "no"

 



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 89fee 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   |