Ansible Tutorials
This page collects practical Ansible workflows instead of isolated command snippets.
A Small Project Layout
First Playbook
- hosts: web
become: true
tasks:
- name: Install nginx
ansible.builtin.package:
name: nginx
state: present
- name: Start nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
Inventory Example
Role Pattern
tasks/main.ymlfor the main task flow.templates/for Jinja2 templates.defaults/main.ymlfor safe defaults.vars/main.ymlfor values that should not be overridden casually.handlers/main.ymlfor restart or reload actions.
Good Workflow
- Start with an ad hoc command to verify connectivity.
- Move repeated logic into a playbook.
- Extract repeated playbooks into a role.
- Add variables and templates only when the shape stabilizes.
- Run the playbook in check mode before applying changes in production.
Practical Notes
- Keep playbooks readable by grouping tasks by outcome.
- Use tags for partial runs during troubleshooting.
- Prefer inventories and variables over hard-coded hostnames.