Ansible Basic Playbook

Ticker

6/recent/ticker-posts

Ansible Basic Playbook

QuestionOne of the Nautilus DevOps team members was working on to test an Ansible playbook on jump host. However, he was only able to create the inventory, and due to other priorities that came in he must work on other tasks. Please pick up this task from where he left off and complete it. Below are more details about the task:

The inventory file /home/thor/ansible/inventory seems to be having some issues; please fix them. The playbook needs to be run on App Server 3 in Stratos DC, so inventory file needs to be updated accordingly.

Create a playbook /home/thor/ansible/playbook.yml and add a task to create an empty file /tmp/file.txt on App Server 3.

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:  

1. Create a inventory file and add the app server as per your task

thor@jump_host /$ vi /home/thor/ansible/inventory                      

thor@jump_host /$ cat /home/thor/ansible/inventory

stapp03 ansible_host=172.16.238.12 ansible_user=banner ansible_ssh_pass=BigGr33n   

thor@jump_host /$    

2. Create a Playbook file 

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

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

- name: Create file in appserver

  hosts: stapp03

  become: yes

  tasks:

    - name: Create the file

      file:

        path: /tmp/file.txt

        state: touch


thor@jump_host /$ 

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

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

stapp03 | CHANGED | rc=0 >>

total 4

drwx------ 2 banner banner 4096 Aug  3 15:20 ansible_command_payload_crUi5o

thor@jump_host ~/ansible$

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

 

PLAY [Create file in appserver] *************************************************

 

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

ok: [stapp03]

 

TASK [Create the file] **********************************************************

changed: [stapp03]

 

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

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

 

thor@jump_host ~/ansible$

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

stapp03 | CHANGED | rc=0 >>

total 4

-rw-r--r-- 1 root   root      0 Aug  3 15:21 file.txt

drwx------ 2 banner banner 4096 Aug  3 15:21 ansible_command_payload_Dcfc5t

thor@jump_host ~/ansible$


4.  Click on Finish & Confirm to complete the task successfully

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