4.Ansible Unarchive Module

4.Ansible Unarchive Module

# ansible# kodekloud
4.Ansible Unarchive ModuleThu Kha Kyawe

Lab Information One of the DevOps team members has created a zip archive on jump host in...

Lab Information

One of the DevOps team members has created a zip archive on jump host in Stratos DC that needs to be extracted and copied over to all app servers in Stratos DC itself. Because this is a routine task, the Nautilus DevOps team has suggested automating it. We can use Ansible since we have been using it for other automation tasks. Below you can find more details about the task:

We have an inventory file under /home/thor/ansible directory on jump host, which should have all the app servers added already.

There is a zip archive /usr/src/itadmin/xfusion.zip on jump host.

Create a playbook.yml under /home/thor/ansible/ directory on jump host itself to perform the below given tasks.

Unzip /usr/src/itadmin/xfusion.zip archive in /opt/itadmin/ location on all app servers.

Make sure the extracted data must has the respective sudo user as their user and group owner, i.e tony for app server 1, steve for app server 2, banner for app server 3.

The extracted data permissions must be 0744.
Enter fullscreen mode Exit fullscreen mode

Note: Validation will try to run the playbook using command ansible-playbook -i inventory playbook.yml so please make sure playbook works this way, without passing any extra arguments.

Lab Solutions

βœ… Part 1: Lab Step-by-Step Guidelines (Technical & Precise)

Step 1: Move to Ansible Directory

cd /home/thor/ansible
Enter fullscreen mode Exit fullscreen mode

Inventory already exists here.

Step 2: Create Playbook

vi /home/thor/ansible/playbook.yml
Enter fullscreen mode Exit fullscreen mode

Add the following:

---
- name: Extract and distribute xfusion archive
  hosts: all
  become: yes

  tasks:

    - name: Ensure destination directory exists
      file:
        path: /opt/itadmin
        state: directory
        mode: '0755'

    - name: Copy zip archive to app servers
      copy:
        src: /usr/src/itadmin/xfusion.zip
        dest: /opt/itadmin/xfusion.zip

    - name: Extract archive on app servers
      unarchive:
        src: /opt/itadmin/xfusion.zip
        dest: /opt/itadmin/
        remote_src: yes

    - name: Set ownership for stapp01
      file:
        path: /opt/itadmin
        owner: tony
        group: tony
        recurse: yes
        mode: '0744'
      when: inventory_hostname == "stapp01"

    - name: Set ownership for stapp02
      file:
        path: /opt/itadmin
        owner: steve
        group: steve
        recurse: yes
        mode: '0744'
      when: inventory_hostname == "stapp02"

    - name: Set ownership for stapp03
      file:
        path: /opt/itadmin
        owner: banner
        group: banner
        recurse: yes
        mode: '0744'
      when: inventory_hostname == "stapp03"
Enter fullscreen mode Exit fullscreen mode

Save and exit.

Step 3: Run the Playbook

ansible-playbook -i inventory playbook.yml
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify Extraction

Run:

ansible all -i inventory -a "ls -l /opt/itadmin"
Enter fullscreen mode Exit fullscreen mode

Output

stapp01 | CHANGED | rc=0 >>
drwxr--r-- 3 tony tony 4096 Mar  6 02:13 /opt/itadmin
stapp02 | CHANGED | rc=0 >>
drwxr--r-- 3 steve steve 4096 Mar  6 02:13 /opt/itadmin
stapp03 | CHANGED | rc=0 >>
drwxr--r-- 3 banner banner 4096 Mar  6 02:13 /opt/itadmin
Enter fullscreen mode Exit fullscreen mode

Step 5: Verify Permissions

ansible all -i inventory -m shell -a "stat -c '%U %G %a %n' /opt/itadmin/*"
Enter fullscreen mode Exit fullscreen mode

Expected:

stapp03 | CHANGED | rc=0 >>
banner banner 744 /opt/itadmin/unarchive
banner banner 744 /opt/itadmin/xfusion.zip
stapp02 | CHANGED | rc=0 >>
steve steve 744 /opt/itadmin/unarchive
steve steve 744 /opt/itadmin/xfusion.zip
stapp01 | CHANGED | rc=0 >>
tony tony 744 /opt/itadmin/unarchive
tony tony 744 /opt/itadmin/xfusion.zip
Enter fullscreen mode Exit fullscreen mode

🧠 Part 2: Simple Step-by-Step Explanation (Beginner Friendly)🎯 What Is the Goal?

We have a file on the jump server:

/usr/src/itadmin/xfusion.zip

We need to:

1️⃣ Send it to every app server
2️⃣ Extract it into /opt/itadmin/
3️⃣ Set correct owner depending on the server
4️⃣ Set permission 744

πŸ“¦ Step 1 – Copy the Archive

The copy module moves the zip file from the jump host to every server.

Think of it like:

Jump Host
↓
App Server 1
App Server 2
App Server 3
πŸ“‚ Step 2 – Extract the Zip File

The unarchive module does the same thing as running:

unzip xfusion.zip

But Ansible performs it automatically on each server.

Important line:

remote_src: yes

This tells Ansible:

β€œThe zip file is already on the remote server.”

πŸ“ Step 3 – Set Correct Owners

Each server has a different main user.

Server Owner
stapp01 tony
stapp02 steve
stapp03 banner

We use when: conditions so each server receives the correct ownership.

πŸ” Step 4 – Set File Permissions

Permission 0744 means:

User Permission
Owner read + write + execute
Group read
Others read

This keeps the files readable but restricts modifications.

πŸ” What Happens When the Playbook Runs?

For each server Ansible:

1️⃣ Connects using SSH
2️⃣ Copies xfusion.zip
3️⃣ Extracts it into /opt/itadmin
4️⃣ Sets correct owner
5️⃣ Sets permission 744

All done automatically across all servers.


Resources & Next Steps
πŸ“¦ Full Code Repository: KodeKloud Learning Labs
πŸ“– More Deep Dives: Whispering Cloud Insights - Read other technical articles
πŸ’¬ Join Discussion: DEV Community - Share your thoughts and questions
πŸ’Ό Let's Connect: LinkedIn - I'd love to connect with you

Credits
β€’ All labs are from: KodeKloud
β€’ I sincerely appreciate your provision of these valuable resources.