Ansible Unarchive Module

Ticker

6/recent/ticker-posts

Ansible Unarchive Module

Question 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 automating tasks. Below you can find more details about the task:

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

There is a ZIP archive /usr/src/data/datacenter.zip on jump host.

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

Unzip /usr/src/data/datacenter.zip archive in /opt/data/ location on all app servers.

Make sure the unzipped 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 0655

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


Please Note :-  Perform the below commands based on your question server,  user name & other details that might differ. So please read the task carefully before executing it. All the Best 👍

Solution:  

1Go through the folder mentioned in task and create inventory & playbook files    

thor@jump_host /$ cd  /home/thor/ansible/

thor@jump_host ~/ansible$

thor@jump_host ~/ansible$ cat inventory

stapp01 ansible_host=172.16.238.10 ansible_ssh_pass=Ir0nM@n ansible_user=tony

stapp02 ansible_host=172.16.238.11 ansible_ssh_pass=Am3ric@ ansible_user=steve

stapp03 ansible_host=172.16.238.12 ansible_ssh_pass=BigGr33n ansible_user=banner

thor@jump_host ~/ansible$


2. Check the existing file & verify the inventory file    

thor@jump_host ~/ansible$ ansible all -a "ls -ltr /opt/data/" -i inventory

stapp01 | CHANGED | rc=0 >>

total 0

stapp02 | CHANGED | rc=0 >>

total 0

stapp03 | CHANGED | rc=0 >>

total 0

thor@jump_host ~/ansible$ ll /usr/src/data/

total 4

-rw-r--r-- 1 root root 367 Jun 15 05:55 datacenter.zip

thor@jump_host ~/ansible$



3.  Create a playbook as per the task  ( refer Video below for clarity )

thor@jump_host ~/ansible$ vi playbook.yml

thor@jump_host ~/ansible$ cat playbook.yml

- name: Extract archive

  hosts: stapp01, stapp02, stapp03

  become: yes

  tasks:

    - name: Extract the archive and set the owner/permissions

      unarchive:

        src: /usr/src/data/datacenter.zip

        dest: /opt/data/

        owner: "{{ ansible_user }}"

        group: "{{ ansible_user }}"

        mode: "0655"

thor@jump_host ~/ansible$


4. Post file saved , run below command to execute the playbook
   

thor@jump_host ~/ansible$ ansible-playbook  -i inventory playbook.yml

 

PLAY [Extract archive] ************************************************************************************

 

TASK [Gathering Facts] ************************************************************************************

ok: [stapp03]

ok: [stapp01]

ok: [stapp02]

 

TASK [Extract the archive and set the owner/permissions] **************************************************

changed: [stapp02]

changed: [stapp01]

changed: [stapp03]

 

PLAY RECAP ************************************************************************************************

stapp01                    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

stapp02                    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

stapp03                    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

 

thor@jump_host ~/ansible$


5. validate the task by running the below command
      

thor@jump_host ~/ansible$ ansible all -a "ls -ltr /opt/data/" -i inventory

stapp01 | CHANGED | rc=0 >>

total 4

drwxr-xr-x 2 tony tony 4096 Jun 15 05:55 unarchive

stapp03 | CHANGED | rc=0 >>

total 4

drwxr-xr-x 2 banner banner 4096 Jun 15 05:55 unarchive

stapp02 | CHANGED | rc=0 >>

total 4

drwxr-xr-x 2 steve steve 4096 Jun 15 05:55 unarchive

thor@jump_host ~/ansible$


6. Click on Finish & Confirm to complete the task successful

Happy Learning!!!!


Apart from this if you need more clarity,  I have made a  tutorial video on this , please go through and share your comments. Like and share the knowledge



Post a Comment

0 Comments

Latest Posts

KodeKloud Kubernetes Security CKS  Lab Challenge 4 |  Audit-policy | Install & configure falco utility | Inspect the API server audit logs and identify the user