This project showcases an Ansible Galaxy-style role to install, configure, and manage Nginx on a remote Linux server. It uses best practices such as handlers, variables, and a modular role-based structure perfect for learning and demonstrating real-world Ansible automation.
ansible-nginx-role/
βββ inventory.ini
βββ site.yml
βββ webserver/
βββ tasks/
β βββ main.yml
βββ handlers/
β βββ main.yml
βββ files/
β βββ index.html
βββ vars/
β βββ main.yml
βββ defaults/
β βββ main.yml
βββ meta/
βββ main.yml
- Installs Nginx
- Deploys a custom index.html web page
- Starts and enables Nginx service
- Uses handlers to restart Nginx on file changes
- Uses variables for flexibility and clean code
- A Linux control node (e.g., Ubuntu EC2) with Python 3 and
pip - SSH access to the target machine
gitinstalledansibleinstalled
sudo apt update
sudo apt install -y python3-pip
pip3 install ansibleβ Verify Ansible Installation:
ansible --versionποΈ How to Use This Project πΉ 1. Clone the Repository
git clone https://github.com/Aniruddhakharve/ansible-nginx-role.git
cd ansible-nginx-roleπΉ 2. Edit Your Inventory File inventory.ini
[web]
<your_target_server_ip> ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsaReplace <your_target_server_ip> and SSH key path as per your setup.
πΉ 3. Run the Playbook
ansible-playbook -i inventory.ini site.ymlπ Role Details
webserver/tasks/main.yml
- Installs Nginx
- Copies a custom HTML page
- Notifies handler to restart Nginx
webserver/handlers/main.yml
- Restarts Nginx when notified
webserver/vars/main.yml
nginx_web_root: /var/www/htmlwebserver/files/index.html
- Contains your custom HTML content served by Nginx.
Each folder inside the webserver/ role has a specific purpose:
| Directory | Purpose |
|---|---|
tasks/ |
Contains the main list of tasks to be executed (e.g., installing nginx). |
handlers/ |
Defines actions (like restarting services) triggered by notify. |
files/ |
Stores static files (like index.html) to be copied to target machines. |
vars/ |
Holds role-specific variables used in tasks (e.g., web root path). |
defaults/ |
Contains default variables that can be overridden by the user. |
meta/ |
Defines metadata about the role (e.g., dependencies, author info). |
π‘ These folders follow the Ansible Galaxy role structure, which helps in making roles reusable, organized, and shareable.
β Output Example After running the playbook, visit:
http://<your_target_server_ip>You should see: π Nginx Configured by Ansible Role
π§ Skills Showcased
- β Role-based Ansible automation
- β Use of handlers, vars, defaults, and files
- β Clean, reusable infrastructure code
- β Real-world provisioning with Ansible Galaxy structure
π§βπ» Author
Aniruddha Kharve
π οΈ MCA Graduate | Linux & DevOps Enthusiast
π GitHub: @Aniruddhakharve
β Show Your Support If you found this helpful, please
β star the repo and share with fellow DevOps learners!