Ansible Playbook Example: Deploying a Web Application
Explanation of the Playbook:
- Hosts: Targets the
webserversgroup, which should be defined in your inventory file. - Become: Executes tasks with administrative (
sudo) privileges. - Vars: Defines variables for use within the playbook.
http_portandmax_clientscan be used in configuration templates. - Tasks:
- Install nginx: Uses the
aptmodule to install Nginx on Debian-based systems. Ensures the package cache is updated before installation. - Upload the nginx configuration file: Uses the
templatemodule to push a customized nginx configuration from a local template (nginx.conf.j2). - Ensure nginx is running: Ensures that the Nginx service is started and enabled to start on boot using the
servicemodule. - Copy website files: Transfers files from a local directory to the web server’s root directory. Sets the appropriate permissions and ownership.
- Install nginx: Uses the
- Handlers:
- restart nginx: Defined to restart Nginx whenever the nginx configuration file changes. This is triggered by the
notifydirective in the template task.
- restart nginx: Defined to restart Nginx whenever the nginx configuration file changes. This is triggered by the
This playbook is a basic example for deploying a static web application using Nginx. It demonstrates the use of modules like apt, template, service, and copy, which are commonly used in real-world scenarios to manage web servers and deploy applications. Handlers are particularly useful for restarting services only when necessary, optimizing resource usage during playbook runs.