DEV Community

Cover image for 1.Troubleshoot and Create Ansible Playbook
Thu Kha Kyawe
Thu Kha Kyawe

Posted on • Edited on

1.Troubleshoot and Create Ansible Playbook

Lab Information

An Ansible playbook needs completion on the jump host, where a team member left off. Below are the details:

The inventory file /home/thor/ansible/inventory requires adjustments. The playbook must run on App Server 3 in Stratos DC. Update the inventory accordingly.

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

Note: Validation will run the playbook using the command ansible-playbook -i inventory playbook.yml. Ensure the playbook works without any additional arguments.

Lab Solutions

Step-by-Step Ansible Lab Instructions

🟩 Step 1 β€” Create the playbook directory

mkdir -p ~/playbook
Enter fullscreen mode Exit fullscreen mode

🟩 Step 2 β€” Create the inventory file

vi ~/playbook/inventory
Enter fullscreen mode Exit fullscreen mode

Add the following content:

[app_servers]
stapp01 ansible_user=tony ansible_password=Ir0nM@n owner_name=tony
stapp02 ansible_user=steve ansible_password=Am3ric@ owner_name=steve
stapp03 ansible_user=banner ansible_password=BigGr33n owner_name=banner
Enter fullscreen mode Exit fullscreen mode

βœ” This defines all app servers
βœ” It also maps each host to the correct owner

🟩 Step 3 β€” Create the playbook file

vi ~/playbook/playbook.yml
Enter fullscreen mode Exit fullscreen mode

Add the following:

---
- name: Create /home/opt.txt on all app servers
  hosts: app_servers
  become: yes

  tasks:
    - name: Ensure /home/opt.txt exists with correct permissions and ownership
      file:
        path: /home/opt.txt
        state: touch
        mode: "0744"
        owner: "{{ owner_name }}"
        group: "{{ owner_name }}"
Enter fullscreen mode Exit fullscreen mode

🟩 Step 4 β€” Verify your files

# Inventory:
cat ~/playbook/inventory

# Playbook:
cat ~/playbook/playbook.yml
Enter fullscreen mode Exit fullscreen mode

🟩 Step 5 β€” Run the playbook (Validation will do this automatically)

ansible-playbook -i inventory playbook.yml
Enter fullscreen mode Exit fullscreen mode

This will:

βœ” Create /home/opt.txt on all app servers
βœ” Set file permissions to 0744
βœ” Assign correct owner/group per server:

stapp01 β†’ tony

stapp02 β†’ steve

stapp03 β†’ banner


Resources & Next Steps
πŸ“¦ Full Code Repository: KodeKloud Learning Labs
πŸ“– More Deep Dives: Whispering Cloud Insights - Read other technical articles
πŸ’¬ Join Discussion: DEV Community - Share your thoughts and questions
πŸ’Ό Let's Connect: LinkedIn - I'd love to connect with you

Credits
β€’ All labs are from: KodeKloud
β€’ I sincerely appreciate your provision of these valuable resources.

Top comments (0)