Ansible Archive Module

Ticker

6/recent/ticker-posts

Ansible Archive Module

Question : The Nautilus DevOps team has some data on jump host in Stratos DC that they want to copy on all app servers in the same data center. However, they want to create an archive of data and copy it to the app servers. Additionally, there are some specific requirements for each server. Perform the task using Ansible playbook as per requirements mentioned below:

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

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


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

Solution:  

1. At first ansible inventory file is working properly and doesn't have any file exist from jump server to all the app server's      

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

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

stapp01 | CHANGED | rc=0 >>

total 0

stapp03 | CHANGED | rc=0 >>

total 0

stapp02 | CHANGED | rc=0 >>

total 0

thor@jump_host ~/ansible$


2.  If you see above output then create playbook file ( refer video for more details )       

thor@jump_host ~/ansible$ vi /home/thor/ansible/playbook.yml

thor@jump_host ~/ansible$ cat /home/thor/ansible/playbook.yml

- name: Task create archive and copy to host

  hosts: stapp01, stapp02, stapp03

  become: yes

  tasks:

    - name: As per the task create the archive file and set the owner

      archive:

        path: /usr/src/sysops/

        dest: /opt/sysops/beta.tar.gz

        format: gz

        force_archive: true

        owner: "{{ ansible_user }}"

        group: "{{ ansible_user }}"

 thor@jump_host ~/ansible$



3.  Run below command to execute the task

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

 PLAY [Task create archive and copy to host] ***************************************************************

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

ok: [stapp01]

ok: [stapp02]

ok: [stapp03]

 TASK [As per the task create the archive file and set the owner] ******************************************

changed: [stapp03]

changed: [stapp01]

changed: [stapp02]

 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$


4.  Validate the task by below command 

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

stapp03 | CHANGED | rc=0 >>

total 4

-rw-r--r-- 1 banner banner 164 Jun 14 05:19 beta.tar.gz

stapp02 | CHANGED | rc=0 >>

total 4

-rw-r--r-- 1 steve steve 176 Jun 14 05:19 beta.tar.gz

stapp01 | CHANGED | rc=0 >>

total 4

-rw-r--r-- 1 tony tony 167 Jun 14 05:19 beta.tar.gz

thor@jump_host ~/ansible$

   
5.  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

3 Comments

  1. Hello. Thank you for your videos and tutorials. Got this error in this task:
    chown failed: failed to look up user $banner", "owner": "root", "path": "/opt/dba/news.tar.gz", "size": 157, "state": "file", "uid": 0}

    ReplyDelete
    Replies
    1. Did you first check the ansible all -a "ls -ltr /opt/sysops" -i inventory , did you facing any issue. . Error seems to be for stapp03 banner user kindly login on the server and check the file path . Still if you face issue connect me

      Delete
  2. Hello. Thank you for your videos and tutorials. Got this error in this task:
    chown failed: failed to look up user $banner", "owner": "root", "path": "/opt/dba/news.tar.gz", "size": 157, "state": "file", "uid": 0}

    ReplyDelete

Latest Posts