Ansible 101: install and core building blocks
This post is a quick orientation to Ansible — what it is, the moving parts you need to know, and a small Nginx example you can actually run. There's a companion GitHub repo at the end.
What is Ansible?
Ansible is an open-source configuration management and automation tool. Sysadmins and DevOps engineers use it to apply configuration to fleets of machines in a repeatable, declarative way. Compared to similar tools (Puppet, Chef, Salt), Ansible's selling point is agentless operation: it talks to target hosts over plain SSH, so there's nothing to install on the target except Python.
What you'd use it for
- Configuration management — keep a fleet of servers in a known, declarative state.
- Automation — replace manual SSH/runbook steps with playbooks.
- Software delivery — push application releases or runtime configs to many hosts at once.
- CI/CD — wire deployment into a pipeline.
Why it's nice
- Simple — YAML config files, no DSL to learn.
- Flexible — thousands of modules ship in the box, and writing your own is straightforward.
- Agentless — SSH and Python on the target is all you need.
- Cross-platform — Linux, macOS, and Windows targets.
Installing Ansible
Ubuntu / Debian
sudo apt-get install ansible -yCentOS / RHEL / Fedora
sudo dnf install epel-release -ysudo dnf install ansible -ymacOS
brew install ansibleOr via pip if you want a specific version: pip install ansible.
The core building blocks
Inventory
The inventory file lists the hosts Ansible will talk to and (optionally) groups and per-host variables. INI is the classic format; YAML works too.
# inventory.ini[servers]server ansible_host=192.168.1.10[servers:vars]nginx_worker_processes=4nginx_worker_connections=1024Playbooks and roles
A playbook is a YAML file that says "run these tasks against these hosts." A role is a reusable bundle of tasks, variables, templates, and handlers — the unit of reuse in Ansible.
---- name: Configure Server hosts: servers roles: - role: nginxTasks, handlers, and variables
- Tasks — individual steps inside a playbook or role.
- Handlers — special tasks that run only when notified (typically to restart a service after its config changes).
- Variables — let you parameterize playbooks; can be set in inventory, role defaults, group/host vars, or on the command line.
A small role:
# roles/nginx/tasks/main.yml---- name: Add Nginx GPG Key apt_key: url: https://nginx.org/keys/nginx_signing.key state: present- name: Add official Nginx repository apt_repository: repo: "deb http://nginx.org/packages/ubuntu/ jammy nginx" state: present- name: Install Nginx apt: name: nginx state: latest- name: Copy Nginx configuration file template: src: nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: - Restart Nginx# roles/nginx/handlers/main.yml---- name: Restart Nginx service: name: nginx state: restartedTemplates
Templates produce dynamic config files using Jinja2. Variables, conditionals, loops — anything Jinja2 supports.
# roles/nginx/templates/nginx.conf.j2user www-data;worker_processes {{ nginx_worker_processes }};error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events { worker_connections {{ nginx_worker_connections }};}http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; # ... more configuration ...}Modules and plugins
- Modules — the units of work Ansible runs on a target (
apt,service,template,copy,user, …). The standard library is huge. - Plugins — extend Ansible itself: connection plugins for non-SSH targets, callback plugins for output formatting, lookup plugins for fetching data, etc.
A real Nginx example
A small project that takes everything above and runs it end-to-end against a fresh Ubuntu/Debian VM, installing and configuring Nginx:
Clone it, point the inventory at a test box, and run:
ansible-playbook -i inventory.ini playbook.ymlReferences
- Ansible Docs — the official reference, from getting-started to advanced.
- r/ansible — community Q&A and patterns.
- GitHub — searching for
ansible role <thing>usually turns up production-grade examples for whatever you're trying to configure.