Ansible Copy Module

Ticker

6/recent/ticker-posts

Ansible Copy Module

Question : There is data on jump host that needs to be copied on all application servers in Stratos DC. Nautilus DevOps team want to perform this task using Ansible only. Perform this task using Ansible as per details mentioned below:

a. On jump host create an inventory file /home/thor/ansible/inventory and add all application servers as managed nodes.

b. On jump host create a playbook /home/thor/ansible/playbook.yml to copy /usr/src/sysops/index.html file to all application servers at location /opt/sysops.

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 the task and create inventory & playbook files    

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

thor@jump_host ~/ansible$ ll

total 0

thor@jump_host ~/ansible$

2.  Create an inventory file and add the app servers as per task.

thor@jump_host ~/ansible$ vi inventory

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$


3.  Check the inventory file is working correctly by listing folder on all the app servers

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

stapp02 | CHANGED | rc=0 >>

total 0

stapp03 | CHANGED | rc=0 >>

total 0

stapp01 | CHANGED | rc=0 >>

total 0

thor@jump_host ~/ansible$


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

thor@jump_host ~/ansible$ vi playbook.yml

thor@jump_host ~/ansible$ cat playbook.yml

- name: Ansible copy

  hosts: all

  become: yes

  tasks:

    - name: copy index.html to sysops folder

      copy: src=/usr/src/sysops/index.html dest=/opt/sysops

thor@jump_host ~/ansible$

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

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

 

PLAY [ansible copy] *********************************************************************************************************

 

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

ok: [stapp03]

ok: [stapp01]

ok: [stapp02]

 

TASK [copy index.html to sysops folder] *************************************************************************************

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$


6. validate the task by running the below command
      

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

stapp02 | CHANGED | rc=0 >>

total 4

-rw-r--r-- 1 root root 35 Jul 29 09:56 index.html

stapp03 | CHANGED | rc=0 >>

total 4

-rw-r--r-- 1 root root 35 Jul 29 09:56 index.html

stapp01 | CHANGED | rc=0 >>

total 4

-rw-r--r-- 1 root root 35 Jul 29 09:56 index.html

thor@jump_host ~/ansible$



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