Home

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

Why it's nice

Installing Ansible

Ubuntu / Debian

bash
sudo apt-get install ansible -y

CentOS / RHEL / Fedora

bash
sudo dnf install epel-release -ysudo dnf install ansible -y

macOS

bash
brew install ansible

Or 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.

ini
# inventory.ini[servers]server ansible_host=192.168.1.10[servers:vars]nginx_worker_processes=4nginx_worker_connections=1024

Playbooks 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.

yaml
---- name: Configure Server  hosts: servers  roles:    - role: nginx

Tasks, handlers, and variables

A small role:

yaml
# 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
yaml
# roles/nginx/handlers/main.yml---- name: Restart Nginx  service:    name: nginx    state: restarted

Templates

Templates produce dynamic config files using Jinja2. Variables, conditionals, loops — anything Jinja2 supports.

nginx
# 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

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:

ansible101 on GitHub

Clone it, point the inventory at a test box, and run:

bash
ansible-playbook -i inventory.ini playbook.yml

References