Ansible Pilot

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


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.


Let’s jump in a real-life playbook to extract an archive in Ansible Playbook with Ansible.

- name: unarchive module demo
  hosts: all
  become: false
    myurl: ""
    - name: extractor presents
          - unzip
          - tar
        state: present
      become: true
- name: extract archive
        src: "{{ myurl }}"
        dest: "/home/devops/"
        remote_src: true
        validate_certs: true

code with ❤️ in GitHub


Now you know how to extract an archive with Ansible. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.


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

Want to keep this project going? Please donate

Follow me

Subscribe not to miss any new releases

August 27, 2021

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress