3.Ansible Archive Module

3.Ansible Archive Module

# kodekloud# ansilble
3.Ansible Archive ModuleThu Kha Kyawe

Lab Information The Nautilus DevOps team has some data on each app server in Stratos DC...

Lab Information

The Nautilus DevOps team has some data on each app server in Stratos DC that they want to copy to a different location. However, they want to create an archive of the data first, then they want to copy the same to a different location on the respective app server. Additionally, there are some specific requirements for each server. Perform the task using Ansible playbook as per requirements mentioned below:

Create a playbook named playbook.yml under /home/thor/ansible directory on jump host, an inventory file is already placed under /home/thor/ansible/ directory on Jump Server itself.

Create an archive cluster.tar.gz (make sure archive format is tar.gz) of /usr/src/dba/ directory ( present on each app server ) and copy it to /opt/dba/ directory on all app servers. The user and group owner of archive cluster.tar.gz should be tony for App Server 1, steve for App Server 2 and banner for App Server 3.
Enter fullscreen mode Exit fullscreen mode

Lab Solutions

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

Step 1: Switch to Ansible Directory

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

Step 2: Create Playbook File

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

Add the following YAML:

---
- name: Archive and copy DBA data
  hosts: all
  become: yes

  tasks:

    - name: Create archive of /usr/src/dba
      archive:
        path: /usr/src/dba/
        dest: /opt/dba/cluster.tar.gz
        format: gz

    - name: Set ownership for stapp01
      file:
        path: /opt/dba/cluster.tar.gz
        owner: tony
        group: tony
      when: inventory_hostname == "stapp01"

    - name: Set ownership for stapp02
      file:
        path: /opt/dba/cluster.tar.gz
        owner: steve
        group: steve
      when: inventory_hostname == "stapp02"

    - name: Set ownership for stapp03
      file:
        path: /opt/dba/cluster.tar.gz
        owner: banner
        group: banner
      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 Archive Exists

ansible all -i inventory -a "ls -l /opt/dba/cluster.tar.gz"
Enter fullscreen mode Exit fullscreen mode

Expected output example:

stapp02 | CHANGED | rc=0 >>
-rw-r--r-- 1 steve steve 217 Mar 2 07:44 /opt/dba/cluster.tar.gz
stapp01 | CHANGED | rc=0 >>
-rw-r--r-- 1 tony tony 209 Mar 2 07:44 /opt/dba/cluster.tar.gz
stapp03 | CHANGED | rc=0 >>
-rw-r--r-- 1 banner banner 202 Mar 2 07:44 /opt/dba/cluster.tar.gz

βœ… What This Playbook Does Technically

Uses archive module (not shell tar command)

Creates tar.gz format

Places archive directly in /opt/dba/

Uses conditional execution (when)

Applies per-host ownership

🧠 Part 2: Simple Step-by-Step Explanation (Beginner Friendly)

🎯 What Is the Goal?

Each app server has a folder:

/usr/src/dba/

We need to:

Compress it into a file called cluster.tar.gz

Put that file inside /opt/dba/

Set the correct owner depending on the server

πŸ“¦ What Is an Archive?

An archive is like:

Zipping a folder into one compressed file.

Instead of many files and folders, you get:

cluster.tar.gz

It’s smaller and easier to move.

πŸ”„ What Happens When the Playbook Runs?

For each server:

Ansible connects

Becomes root

Compresses /usr/src/dba/

Saves it as /opt/dba/cluster.tar.gz

Checks which server it is

Sets the correct owner

🧠 Why Use when: Conditions?

Each server needs a different owner:

Server Owner
stapp01 tony
stapp02 steve
stapp03 banner

The when: condition tells Ansible:

β€œOnly run this task on this specific server.”

So each server gets the correct ownership.

βš™ Why Use archive Module Instead of tar Command?

Because:

It is idempotent (safe to re-run)

Cleaner

Professional Ansible practice

No need for manual shell commands


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.