How to Automate Your Deployment With Ansible: A Practical Guide
Automating deployments saves time and reduces errors. Ansible uses simple YAML playbooks to configure servers, deploy applications, and manage infrastructure—all without agents. This tutorial walks you through automating a typical web application deployment.
1. Set Up Your Ansible Environment
Install Ansible on a control node (your local machine or a CI server). Create an inventory file listing your target servers:
- web1 ansible_host=192.168.1.10 ansible_user=deploy
- web2 ansible_host=192.168.1.11 ansible_user=deploy
Test connectivity with ansible all -i inventory -m ping. Ensure SSH key-based access is set up.
2. Write a Deployment Playbook
Create a playbook, e.g., deploy.yml. It defines tasks in order:
- Update packages (apt/yum module)
- Copy application files (copy or synchronize module)
- Restart the web server (systemd module)
Example task:
- name: Deploy app
copy:
src: /local/app/
dest: /var/www/html/
owner: www-data
3. Run and Verify the Deployment
Execute the playbook:
ansible-playbook -i inventory deploy.yml
Use --check for a dry run. After completion, verify the service is running with ansible web -a "systemctl status nginx".
4. Add Variables and Handlers
Make your playbook reusable with variables (e.g., vars/main.yml) and handlers to trigger service restarts only when configs change.
- Define
app_version: "1.2.3"in vars. - Use
notify: restart nginxon file changes.
Conclusion: Ansible turns repetitive deployment steps into a repeatable, version‑controlled workflow. With inventories, playbooks, and a few modules, you can automate any environment. Start simple, then expand to roles and dynamic inventories for complex infrastructures.