FreeKB - Ansible get_url module
Ansible - get_url module

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.

The ansible-doc get_url command can be used to show the Ansible documention on the get_url module.

This module is excuted on the managed node (e.g. the target system), meaning that file will be downloaded to a directory on the target system .Or, the local_action module can be used to invoke this module on the control node (that's your Ansible server).

- name: "download foo.txt"
  get_url:
    url: "http://example.com/foo.txt"
    dest: "/tmp/foo.txt"
  register: out

 

The debug module can the be used to print the output. Notice the double curley braces {{ ... }}. Jinja2 uses double curley braces for variables.

- name: "output the content of the 'out' variable"
  debug: 
    msg: "{{ 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: 
    msg: "{{ out.status }}"

 

If the remote system requires basic authentication, the force_basic_authurl_username and url_password options 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.

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

 

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"
    owner: "foo"
    group: "foo"
    mode: "0644"
    seuser: "unconfined_u"
    serole: "object_r"
    setype: "httpd_sys_content_r"
    selevel: "s0"

 



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 95a89 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   |