Download a file using an HTTPS proxy via environment variables - Ansible get_url and environment
How to automate the download of a file and verify the checksum using an HTTPS proxy with Ansible module get_url and "https_proxy" environment statement.


How to download a file using an HTTPS proxy via environment variables with Ansible?
I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Download a file using an https proxy via env variables
get_url
module- http_proxy and https_proxy environment
The easiest way to download a file using an HTTPS proxy is via the get_url
Ansible module and the environment variables.
You could set the remote proxy via the http_proxy
and https_proxy
remote environment using the Ansible statement environment
.
This applies respectively to HTTP and HTTPS connections.
The Ansible environment
statement could be applied at the task level or play level.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
Download a file using an HTTPS proxy via environment variables with Ansible Playbook. The following scenario uses the HTTPS proxy server http://proxy.example.com:3128.
code
---
- name: get_url module with proxy demo
hosts: all
become: false
gather_facts: false
vars:
myurl: "https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz"
mycrc: "sha256:https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz.sha"
mydest: "/home/devops/ansible-2.9.25.tar.gz"
tasks:
- name: download file with proxy
ansible.builtin.get_url:
url: "{{ myurl }}"
dest: "{{ mydest }}"
checksum: "{{ mycrc }}"
mode: '0644'
owner: devops
group: wheel
environment:
https_proxy: "http://proxy.example.com:3128"
execution
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory download\ file/get_url_with_proxy.yml
PLAY [get_url module with proxy demo] *************************************************************
TASK [download file with proxy] *******************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory download\ file/get_url_with_proxy.yml
PLAY [get_url module with proxy demo] *************************************************************
TASK [download file with proxy] *******************************************************************
ok: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
before execution
ansible-pilot $ ssh [email protected]
Last login: Mon Feb 21 14:34:39 2022 from 192.168.251.111
[devops@demo ~]$ ls -al
total 16
drwx------. 4 devops wheel 111 Feb 21 14:29 .
drwxr-xr-x. 4 root root 35 Jan 5 10:18 ..
drwx------. 3 devops wheel 17 Jan 5 10:22 .ansible
-rw-------. 1 devops wheel 560 Feb 21 14:33 .bash_history
-rw-r--r--. 1 devops wheel 18 Jul 26 2021 .bash_logout
-rw-r--r--. 1 devops wheel 141 Jul 26 2021 .bash_profile
-rw-r--r--. 1 devops wheel 376 Jul 26 2021 .bashrc
drwx------. 2 devops wheel 29 Jan 5 10:18 .ssh
[devops@demo ~]$
after execution
ansible-pilot $ ssh [email protected]
Last login: Mon Feb 21 14:36:10 2022 from 192.168.251.111
[devops@demo ~]$ ls -al
total 13964
drwx------. 4 devops wheel 140 Feb 21 14:36 .
drwxr-xr-x. 4 root root 35 Jan 5 10:18 ..
drwx------. 3 devops wheel 17 Jan 5 10:22 .ansible
-rw-------. 1 devops wheel 572 Feb 21 14:35 .bash_history
-rw-r--r--. 1 devops wheel 18 Jul 26 2021 .bash_logout
-rw-r--r--. 1 devops wheel 141 Jul 26 2021 .bash_profile
-rw-r--r--. 1 devops wheel 376 Jul 26 2021 .bashrc
drwx------. 2 devops wheel 29 Jan 5 10:18 .ssh
-rw-r--r--. 1 devops wheel 14280306 Feb 21 14:36 ansible-2.9.25.tar.gz
[devops@demo ~]$
ansible-pilot $ ssh [email protected]
[devops@proxy ~]$ sudo su
[root@proxy devops]# cat /var/log/squid/access.log
1645453749.115 598 192.168.251.205 TCP_TUNNEL/200 3952 CONNECT releases.ansible.com:443 - HIER_DIRECT/104.26.1.234 -
1645453770.840 412 192.168.251.205 TCP_TUNNEL/200 3955 CONNECT releases.ansible.com:443 - HIER_DIRECT/104.26.1.234 -
1645453779.292 8442 192.168.251.205 TCP_TUNNEL/200 14304919 CONNECT releases.ansible.com:443 - HIER_DIRECT/104.26.1.234 -
1645454103.403 135 192.168.251.205 TCP_TUNNEL/200 39 CONNECT releases.ansible.com:443 - HIER_DIRECT/104.26.0.234 -
1645454157.121 501 192.168.251.205 TCP_TUNNEL/200 3957 CONNECT releases.ansible.com:443 - HIER_DIRECT/104.26.1.234 -
1645454163.543 6413 192.168.251.205 TCP_TUNNEL/200 14304902 CONNECT releases.ansible.com:443 - HIER_DIRECT/104.26.1.234 -
1645454171.680 497 192.168.251.205 TCP_TUNNEL/200 3948 CONNECT releases.ansible.com:443 - HIER_DIRECT/104.26.1.234 -
[root@proxy devops]#
Recap
Now you know how to use HTTPS proxy using environment variables with Ansible Playbook. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack 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
Donate
Want to keep this project going? Please donate