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.
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
Inventory already exists here.
Step 2: Create Playbook
vi /home/thor/ansible/playbook.yml
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"
Save and exit.
Step 3: Run the Playbook
ansible-playbook -i inventory playbook.yml
Step 4: Verify Extraction
Run:
ansible all -i inventory -a "ls -l /opt/itadmin"
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
Step 5: Verify Permissions
ansible all -i inventory -m shell -a "stat -c '%U %G %a %n' /opt/itadmin/*"
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
π§ 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.
Top comments (0)