Skip to main content

Command Palette

Search for a command to run...

Day 83: Troubleshoot and Create Ansible Playbook | 100 Days of DevOps

Updated
โ€ข5 min read
R
Iโ€™m currently working in DevOps and documenting my learning journey along the way. From CI/CD pipelines to cloud and containers, Iโ€™m exploring how modern systems are built and deployed. This blog is where I share what I learn, break down concepts in simple terms, and track my progress. Learning one concept at a time, and trying to apply it practically.

Content

Today I worked on troubleshooting an existing Ansible configuration and creating a playbook to perform a simple file management task on a remote application server. This task helped me understand how inventories are structured, how Ansible groups hosts, and how playbooks can be used to create files on remote systems.


๐Ÿ”น What I Learned

  • Troubleshooting Ansible inventory files
  • Creating inventory groups
  • Configuring host authentication variables
  • Using the file module to manage files remotely
  • Creating files using state: touch
  • Running and validating Ansible playbooks

๐Ÿ”น Task Requirement

As per the Nautilus DevOps team requirements, I needed to:

  1. Review and fix the inventory file located at:
/home/thor/ansible/inventory
  1. Ensure the playbook runs on App Server 3 in the Stratos Datacenter.

  2. Create a playbook:

/home/thor/ansible/playbook.yml
  1. Add a task to create an empty file:
/tmp/file.txt

on App Server 3.

  1. Ensure the solution works using:
ansible-playbook -i inventory playbook.yml

without requiring any additional arguments.


๐Ÿ”น Steps I Followed

1. Connected to the Working Directory

Moved into the Ansible project directory:

cd /home/thor/ansible/

Verified available files:

ls

Output:

inventory

2. Reviewed the Existing Inventory

Checked the current inventory file:

cat inventory

Output:

stapp03 ansible_user=banner ansible_ssh_pass=$pwd ansible_ssh_common_args='-o StrictHostKeyChecking=no'

I noticed two issues:

  • The password variable $pwd was not a valid password value.
  • No inventory group was defined.

Since the playbook would target a host group, I needed to organize the server into an inventory group.


3. Updated the Inventory File

Edited the inventory:

vi inventory

Updated content:

[app]
stapp03 ansible_user=banner ansible_ssh_pass=BigGr33n ansible_ssh_common_args='-o StrictHostKeyChecking=no'

Saved and verified:

cat inventory

Output:

[app]
stapp03 ansible_user=banner ansible_ssh_pass=BigGr33n ansible_ssh_common_args='-o StrictHostKeyChecking=no'

๐Ÿ”น Simple Explanation of the Inventory Entry

Inventory Group

[app]

Defines a host group named app.

Playbooks can target this group instead of individual hosts.


Inventory Host

stapp03

Represents App Server 3.

This is the server Ansible will manage.


SSH User

ansible_user=banner

Specifies the remote user account used for authentication.


SSH Password

ansible_ssh_pass=BigGr33n

Provides the password used to connect to the server.


SSH Argument

ansible_ssh_common_args='-o StrictHostKeyChecking=no'

Disables SSH host key verification prompts.

This prevents Ansible from stopping for manual confirmation when connecting to a host for the first time.


Complete Inventory Entry

[app]
stapp03 ansible_user=banner ansible_ssh_pass=BigGr33n ansible_ssh_common_args='-o StrictHostKeyChecking=no'

๐Ÿ‘‰ In simple terms:

This inventory tells Ansible:

  • Which server to manage
  • Which user account to use
  • What password to authenticate with
  • To skip SSH host key confirmation prompts

4. Created the Playbook

Created the playbook file:

vi /home/thor/ansible/playbook.yml

Added the following content:

---
- name: Create a file on App server3
  hosts: app
  become: true

  tasks:
    - name: create an empty file /tmp/file.txt
      ansible.builtin.file:
        path: /tmp/file.txt
        state: touch
        mode: '0644'

Saved the file.


๐Ÿ”น Understanding the Playbook

Target Hosts

hosts: app

The playbook targets all hosts within the app inventory group.

Since only App Server 3 exists in this group, the task runs only on that server.


Privilege Escalation

become: true

Allows Ansible to execute tasks with elevated privileges.


File Module

ansible.builtin.file

The file module is used to manage files and directories on remote systems.


Creating an Empty File

state: touch

Works similarly to the Linux touch command.

If the file does not exist, it is created.

If it already exists, only its timestamp is updated.


File Permissions

mode: '0644'

Sets the file permissions to:

  • Owner: Read + Write
  • Group: Read
  • Others: Read

5. Executed the Playbook

Ran the playbook:

ansible-playbook -i inventory playbook.yml

Output:

PLAY [Create a file on App server3]

TASK [Gathering Facts]
ok: [stapp03]

TASK [create an empty file /tmp/file.txt]
changed: [stapp03]

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

๐Ÿ”น Understanding the Output

Gathering Facts

TASK [Gathering Facts]

Ansible collects system information from the target host before executing tasks.

Examples include:

  • Hostname
  • Operating system
  • Network information
  • Memory details

File Creation Task

changed: [stapp03]

Indicates the task successfully created the file.


Play Recap

ok=2
changed=1
failed=0

Meaning:

  • Two tasks completed successfully
  • One task modified the system
  • No failures occurred

๐Ÿ”น Inventory vs Playbook

One important concept reinforced during this task was the separation between inventory and playbook responsibilities.

Inventory

Defines:

  • Which systems to manage
  • Authentication details
  • Connection settings
  • Host groups

Example:

[app]
stapp03 ansible_user=banner ansible_ssh_pass=BigGr33n

Playbook

Defines:

  • What actions should be performed
  • Which modules should be used
  • Which host groups should receive the tasks

Example:

hosts: app

and

state: touch

๐Ÿ”น My Understanding

This task strengthened my understanding of how Ansible inventories and playbooks work together.

The inventory defines the infrastructure and connection details, while the playbook contains the automation logic. By organizing hosts into groups, playbooks become reusable and easier to manage across multiple servers.


๐Ÿ”น What I Found Interesting

I found it interesting that a simple file creation task required proper inventory configuration before the playbook could run successfully. This reinforced the idea that in Ansible, connectivity and inventory management are just as important as the automation tasks themselves.

I also learned how the file module can mimic common Linux commands like touch, making infrastructure automation both readable and efficient.

๐Ÿ“Œ Full notes: GitHub link

100_days_of_devops

Part 1 of 50

Documenting my 100 Days of DevOps journey with hands-on practice from KodeKloud, along with notes, commands, and mini projects