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.
Lab Solutions
β Part 1: Lab Step-by-Step Guidelines (Technical & Precise)
Step 1: Switch to Ansible Directory
cd /home/thor/ansible
Step 2: Create Playbook File
vi /home/thor/ansible/playbook.yml
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"
Save and exit.
Step 3: Run the Playbook
ansible-playbook -i inventory playbook.yml
Step 4: Verify Archive Exists
ansible all -i inventory -a "ls -l /opt/dba/cluster.tar.gz"
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
Top comments (0)