Today I dived deeper into Ansible, moving from simple ad-hoc commands to full-blown Playbooks and Roles. Here is what I built! π οΈ
π The Playbook
I started by writing a basic YAML playbook to automate which Installs apache2 and copies the index.html page to /var/www/html
and runs the webserver
The beauty of it? If I run it twice, Ansible knows nothing needs to change. That idempotency is a game-changer. It was satisfying to see the tasks execute sequentially:
YAML
- name: Installing and running apache2
hosts: all
roles:
- httpd
π¦ Leveling Up with Roles
I quickly realized that a single playbook file gets hard to read. I learned about Ansible Roles, which is the standard way to organize automation code.
I created my first role structure:
roles/
βββ my-app/
βββ tasks/
β βββ main.yml
βββ handlers/
β βββ main.yml
βββ vars/
βββ main.yml
This makes the code reusable. If I need this configuration again, I just call the role!
π The Code
Iβve pushed my first role to GitHub. Iβm trying to keep my commits clean and my structure organized.
Check it out here: https://github.com/JayanthDasari7/Ansible-Roles
linkedinΒ :https://www.linkedin.com/in/dasari-jayanth-b32ab9367/
Has anyone else here struggled with the directory structure of Roles initially? Let me know in the comments! π
Top comments (0)