AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible unarchive Module: Extract tar, zip & gz Archives (ansible.builtin.unarchive) — Video Tutorial
Complete guide to ansible.builtin.unarchive module. Extract tar.gz, zip, and bz2 archives on remote hosts with examples, creates parameter, and remote_src.
What You'll Learn
- How to Extract an Archive in Ansible?
- Basic Usage
- Extract Local Archive to Remote Host
- Extract Archive Already on Remote Host
- Download and Extract from URL
- Parameters Reference
- Idempotent Extraction
- Using creates Parameter
- Using stat + when Conditional
- Include and Exclude Files
Full Tutorial Content
The `ansible.builtin.unarchive` module extracts compressed archives (tar, tar.gz, tar.bz2, tar.xz, zip) on remote hosts. It copies from the Ansible controller or downloads from a URL, extracts to a target directory, and handles permissions and ownership — all in a single task.
How to Extract an Archive in Ansible?
I'm going to show you a live Playbook and some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
The full name is `ansible.builtin.unarchive`, part of the builtin collection shipped with Ansible. It's stable, works across Linux distributions, and handles `.zip` files using `unzip` as well as `.tar`, `.tar.gz`, `.tar.bz2`, `.tar.xz`, and `.tar.zst` files using `gtar`.
For Windows targets, use the `community.windows.win_unzip` module instead.
Basic Usage
Extract Local Archive to Remote Host
By default, `src` is a path on the Ansible controller. The archive is copied to the remote host, then extracted:
```yaml
- name: Extract application archive
ansible.builtin.unarchive:
src: files/myapp-2.1.0.tar.gz
dest: /opt/myapp/
owner: appuser
group: appuser
mode: '0755'
become: true
```
Extract Archive Already on Remote Host
When the archive is already on the remote host, set `remote_src: true`:
```yaml
- name: Extract remote archive
ansible.builtin.unarchive:
src: /tmp/myapp-2.1.0.tar.gz
dest: /opt/myapp/
remote_src: true
become: true
```
Download and Extract from URL
Set `remote_src: true` with an HTTP/HTTPS URL as `src` to download and extract in one step:
```yaml
- name: Download and extract from URL
ansible.builtin.unarchive:
src: https://github.com/prometheus/prometheus/releases/download/v2.52.0/prometheus-2.52.0.linux-amd64.tar.gz
dest: /opt/
remote_src: true
become: true
```
Parameters Reference
| Parameter | Type | Description | Default |
|-----------|------|-------------|---------|
| `src` | string | Archive path (local, remote, or URL) | **Required** |
| `dest` | string | Target extraction directory | **Required** |
| `remote_src` | boolean | Source is on remote host or URL | `false` |
| `owner` | string | Set owner of extracted files | — |
| `group` | string | Set group of extracted files | — |
| `mode` | string | Set permissions of extracted files | — |
| `creates` | string | Skip if this path exists (idempotency) | — |
| `exclude` | list | Files/dirs to exclude from extraction | — |
| `include` | list | Files/dirs to include in extraction | — |
| `keep_newer` | boolean | Don't overwrite newer files on remote | `false` |
| `extra_opts` | list | Extra command-line options for tar/unzip | — |
| `list_files` | boolean | Return list of extracted files in result | `false` |
| `validate_certs` | boolean | Validate SSL certs for HTTPS URLs | `true` |
Idempotent Extraction
Using creates Parameter
The `creates` parameter is the recommended way to make extraction idempotent:
```yaml
- name: Extract application (idempotent)
ansible.b
About This Tutorial
- Author: Luca Berton
- Difficulty: Intermediate
- Read time: 8 min
- Category: installation
Read the full written article: Ansible unarchive Module: Extract tar, zip & gz Archives (ansible.builtin.unarchive)
Related Video Tutorials
- Ansible win_unzip: Extract ZIP Archives on Windows (Examples & Playbook) — How to extract ZIP archives on Windows with Ansible win_unzip module. Unzip files, handle passwords, clean up, and deploy applications with practical examples.
- Ansible script Module: Run Local Scripts on Remote Hosts Guide — How to run local scripts on remote hosts with Ansible script module. Execute Python, Bash, and custom scripts without copying them first. Examples included.
- Ansible Set File Permissions 755: chmod with file Module Guide — How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.
- Ansible 2.17.0-rc1: Elevating Automation with ‘Gallows Pole’ — Ansible 2.17.0-rc1, codenamed "Gallows Pole," introduces critical updates including phasing out support for older Python versions, tightened security measures, and enhanced functionality. The release aims to streamline performance, boost security, and improve user experience, marking a significant advancement in the Ansible automation platform.
- Ansible Troubleshooting Installation Issues on macOS and Python — Learn how to resolve the ImportError for Jinja2 when installing Ansible on macOS using Homebrew, ensuring smooth automation setup.
- Ansible Permission Denied (Errno 13): Fix File Access Errors — Fix Ansible Permission denied [Errno 13] errors. Resolve file permission, become/sudo, SELinux, and directory access issues with troubleshooting steps.