Ansible Pilot

Download a file in Windows-like systems - Ansible module win_get_url

How to automate the download of a file, verify the checksum and save wherever you want in Windows-like systems with Ansible module win_get_url.

February 3, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to download a file in Windows-like systems with Ansible?

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible download a file in Windows-like systems

Let’s talk about the Ansible module win_get_url. The full name is ansible.windows.win_get_url, which means that is part of the collection of modules of Ansible to interact with Windows nodes. It downloads files from HTTP, HTTPS, or FTP to node For Linux targets, use the ansible.builtin.get_url Ansible module instead.

Parameters

The parameters of the win_get_url module. The two required parameters are url and dest. The url parameter specifies the URL of the resource you’re going to download. The dest parameter specifies the filesystem path where the resource is going to be saved on the target node. Let’s deep dive into the “force” parameter. If “dest” is a file, Ansible is going to download every time the file. If “dest” is a directory the default behavior is not to replace a file, until you toggle to yes the force parameter. The parameter “checksum” is very useful to validate the consistency of the downloaded file. You could specify the algorithm under checksum_algorithm, default SHA1, and directly the checksum or the checksum URL checksum_url. Another interesting parameter is “headers” which allows you to specify some custom HTTP headers. Ansible presents himself as “ansible-httpget” in the web server logs, but you cust customize it in the “http_agent” parameter. There are also additional parameters for the X509 authentication client certificate (.pfx). Enterprise users could also specify the proxy settings parameters.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

demo

How to download a file in Windows-like systems with Ansible Playbook.

code

---
- name: win_get_url module demo
  hosts: all
  become: false
  vars:
    myurl: "https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz"
    mycrc: "https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz.sha"
    mycrc_algorithm: "sha256"
    mydest: 'C:\Users\vagrant\Desktop\ansible-2.9.25.tar.gz'
  tasks:
    - name: download file
      ansible.windows.win_get_url:
        url: "{{ myurl }}"
        dest: "{{ mydest }}"
        checksum_algorithm: "{{ mycrc_algorithm }}"
        checksum_url: "{{ mycrc }}"

execution

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory download\ file/win_get_url.yml
PLAY [win_get_url module demo] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [download file] ******************************************************************************
changed: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

idempotency

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory download\ file/win_get_url.yml
PLAY [win_get_url module demo] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [download file] ******************************************************************************
ok: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

before execution

win_get_url before execution

after execution

win_get_url after execution

code with ❤️ in GitHub

Recap

Now you know how to download a file in Windows-like systems with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases