Ansible Unarchive Module Examples

What is Ansible Unarchive Module and How does it work

Ansible Unarchive module helps to unpack or uncompress the files from an archive file such as tar, tar.gz, zip  . Ansible unarchive module can optionally copy the files to the remote server before uncompressing them.

Though the unarchive module is part of Ansible. this module uses the basic unzip and tar -xvf command-line tools to perform the operation. So the target server must have these commands installed. Since most of the *nix distributions are having these tools built-in. I think you can count on it.

Some Quick notes before we see an example of this module

  • This module requires zipinfo and gtar/unzip command on the target remote host.
  • Can handle .zip files using unzip as well as .tar, .tar.gz, .tar.bz2 and .tar.xz files using gtar.
  • Does not handle .gz files, .bz2 files or .xz files that do not contain a .tar archive.
  • It Uses gtar’s --diff arg to calculate if changed or not. If this arg is not supported, it will always unpack the archive.

So I guess this gives you a clear understanding of how Ansible unarchive module does the job in the background.

Before going to ansible unarchive. If you would like to know how to create archive with ansible. refer the below post

Ansible Archive Examples - Zip files and Directories

 

Ansible Unarchive module examples

let us suppose that you have a playbook with two tasks as follows

  1. Copy the tar file
  2. uncompress it with tar -xvf command.

Unarchive could do it as a Single task as it can optionally copy the file from control machine to target remote server and uncompress it there.

Ansible unarchive

In the preceding Snapshot, you can see how the playbook with two tasks became a Single task with help of unarchive.

The Example playbooks with and without unarchive module are given below.

 

Playbook without Unarchive - A usual approach

In this playbook, we use the usual  copy module to copy the files over to the remote server and uncompress it using the tar -xvf command

 

---
- name: Playbook to copy file and uncompress
  hosts: appservers
  vars:
    - userid : "weblogic"
    - oracle_home: "/opt/oracle"
    - jdk_instl_file: "server-jre-8u191-linux-x64.tar.gz"

  tasks:
  - name : Copy the Java JDK files
    become: yes
    become_user: "{{ userid }}"
    tags: app,cpbinaries
    copy:
      src: "{{ item }}"
      dest: "{{ oracle_home }}"
      mode: 0755
    with_items:
      - "{{ jdk_instl_file }}"

  - name: Install java
    become: yes
    become_user: "{{ userid }}"
    tags: javainstall
    shell: "tar xvfz {{ oracle_home }}/{{ jdk_instl_file }}"
    args:
      chdir: "{{ oracle_home }}"
    register: javainstall

 

 

Playbook with unArchive - A Difference factor

Here we do the same job what we did earlier but little smart with help of ansible's unarchive module

 

---
- name: Playbook to copy file and uncompress
  hosts: appservers
  vars:
    - userid : "weblogic"
    - oracle_home: "/opt/oracle"
    - jdk_instl_file: "server-jre-8u191-linux-x64.tar.gz"

  tasks:
  - name : Copy and Install Java
    become: yes
    become_user: "{{ userid }}"
    tags: javainstall
    unarchive:
      src: "{{ item }}"
      dest: "{{ oracle_home }}"
      mode: 0755
    with_items:
      - "{{ jdk_instl_file }}"

In a Simple playbook like this we may not measure the value of reducing a task but when it comes to big roles reducing a Number of tasks means something.

Now we know how this unarchive module is working and this playbook has set some context. Now let us see the other features of Ansible unarchive module.

 

Download a Zip file from remote URL and decompress using unarchive

This is an exciting and wonderful feature of Ansible unarchive module, as it can take URLs as a source of zip file and download it and uncompress it.

Here is a quick example playbook which performs the following tasks

  1. Install OpenJDK Java
  2. Create a new Directory
  3. Download the tar.gz file of Tomcat8 binaries
  4. Un-tar or uncompress the tomcat into the directory created
  5. Start the Tomcat

 

The Playbook

---
- name: Playbook to download and install tomcat8
  hosts: appservers

  tasks:
  - name: install Java
    become: yes
    yum:
      name: java-1.8.0-openjdk-devel
      state: present

  - name: crate a directory
    become: yes
    file:
      path: "/opt/tomcat8"
      state: directory
      mode: 0755

  - name : Download and install tomcat
    become: yes
    tags: installtc
    unarchive:
      src: "http://apachemirror.wuchna.com/tomcat/tomcat-8/v8.5.49/bin/apache-tomcat-8.5.49.tar.gz"
      dest: "/opt/tomcat8/"
      mode: 0755
      remote_src: yes
    register: "tcinstall"

  - name: Start the tomcat instance
    become: yes
    shell:
      "./startup.sh"
    args:
      chdir: "/opt/tomcat8/apache-tomcat-8.5.49/bin"

 

The Execution Output - A Screen Record

What is this remote_src in ansible unarchive module

By this time you would have understood what is this remote_src is but if you are still having a question. This is what it is. remote_src tells the unarchive module to look for the source file on the remote server unless otherwise unarchive would check for the file on the control machine and try to copy it.

Ansible unarchive is by default designed to copy the file mentioned in the src from the control machine ( where Ansible is installed ) to the remote server.  Sometimes we do not want to copy the files or our files would somehow be already present on the remote target server. in that case, you can use this remote_src option.

 

Hope this article gives you some introduction about Ansible unarchive module through examples. Let me know your questions on comments.

Rate this article [ratings]

Cheers
Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading