Skip to content

Ansible Tutorials

This page collects practical Ansible workflows instead of isolated command snippets.

A Small Project Layout

ansible/
  ansible.cfg
  inventory.ini
  group_vars/
  host_vars/
  roles/
  site.yml

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

[web]
web-01 ansible_host=10.0.0.11
web-02 ansible_host=10.0.0.12

[web:vars]
http_port=80

Role Pattern

  • tasks/main.yml for the main task flow.
  • templates/ for Jinja2 templates.
  • defaults/main.yml for safe defaults.
  • vars/main.yml for values that should not be overridden casually.
  • handlers/main.yml for restart or reload actions.

Good Workflow

  1. Start with an ad hoc command to verify connectivity.
  2. Move repeated logic into a playbook.
  3. Extract repeated playbooks into a role.
  4. Add variables and templates only when the shape stabilizes.
  5. 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.