Extract an archive - Ansible module unarchive
Live console coding how to download a zip file and extract it in a user home directory. The playbook is also taking care of the necessary softwre dependencies.


How to extract an archive in 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 extract an archive
Today we’re talking about the Ansible module unarchive
.
The full name is ansible.builtin.unarchive
, which means that is part of the collection of modules “builtin” with ansible and shipped with it.
It’s a module pretty stable and out for years.
It works in a different variety of operating systems.
It unpacks one archive after (optionally) copying it from the local machine
It can handle .zip
files using unzip
as well as .tar
, .tar.gz
, .tar.bz2
, .tar.xz
, and .tar.zst
files using gtar
. It requires zipinfo
,gtar
and unzip
command on target host.
Please note that it requires a tarball (.tar
archive) for the Unix file format.
For Windows targets, use the community.windows.win_unzip
.
Parameters
- src string - path
- dest string - path
- remote_src boolean - no/yes
- validate_certs boolean - no/yes (https only)
- include/exclude list - directory and file entries
- extra_opts string - command-line options
- keep_newer boolean - no/yes
- mode/owner/group - permission
- setype/seuser/selevel - SELinux
The parameter list is pretty wide but this three are the most important options. The only mandatory parameters are “src” and “dst” which are the source and destination paths. The “src” is quite special because is supposed to be a local path on the Ansible controller. We could switch to a remote path enabling the “remote_src” properties. In the “src” parameter we could also enter a URL, so the archive is going to be downloaded before being expanded. Other useful parameters are “include” and “exclude” that allows us to specify a subset of the archive to extract, specifically a list of inclusion or exclusion files or directories. Just in case we need some extra command-line options we could specify in the “extra_opts” parameter. If the files are already present on the target machine the default behavior is to not replace existing files that are newer than files from the archive, you could override enabling the “keep_newer” parameter. Let me also highlight that we could also specify some permissions or SELinux properties.
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
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 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
Let’s jump in a real-life playbook to extract an archive in Ansible Playbook with Ansible.
- unarchive.yml
---
- name: unarchive module demo
hosts: all
become: false
vars:
myurl: "https://github.com/lucab85/ansible-pilot/archive/refs/heads/master.zip"
tasks:
- name: extractor presents
ansible.builtin.yum:
name:
- unzip
- tar
state: present
become: true
- name: extract archive
ansible.builtin.unarchive:
src: "{{ myurl }}"
dest: "/home/devops/"
remote_src: true
validate_certs: true
Recap
Now you know how to extract an archive with Ansible. 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