What Ansible Is
In Linux Server Administration you hardened one server by hand: an admin user, an sshd_config drop-in, a firewall, a sandboxed service, log rotation. Doing it again on the second server takes an afternoon; on the fiftieth it takes a team, and no two servers end up quite the same. Configuration management fixes that: you write down how a server should be configured, and a tool makes every server match, as often as you like.
Ansible is the most widely used configuration management tool today. It is open source (GPLv3), written in Python and maintained by Red Hat together with a large community.
- name: Every web server gets the same motd and nginx config
hosts: web
tasks:
- name: Message of the day
ansible.builtin.copy:
dest: /etc/motd
content: "Managed by Ansible. Changes here are overwritten.\n"
mode: "0644"The ideas that make Ansible different
| Idea | What it means |
|---|---|
| Agentless | nothing to install on the managed servers except Python and an SSH server; the control node (your laptop or a CI runner) connects over SSH, copies a small module, runs it and deletes it |
| Push | you run ansible-playbook when you want a change; nothing polls a central server (Chef and Puppet agents pull every 30 minutes by default) |
| Declarative tasks | most tasks describe a state ("this file has this content", "this package is present"), and the module only acts when the server differs |
| Idempotent | running the same playbook twice changes nothing the second time; the run report tells you changed=0 |
| YAML | playbooks are YAML files, readable by people who never wrote Python |
| Batteries included | thousands of modules in collections: files, packages, users, cloud APIs, network switches, Windows, Kubernetes |
The pieces
| Piece | What it is | Lesson |
|---|---|---|
| Control node | the machine where Ansible is installed and runs | Install Ansible |
| Managed nodes (hosts) | the servers Ansible configures | |
| Inventory | the list of hosts, sorted into groups, with variables | Static Inventories |
| Module | a unit of work: copy, apt, service, user, … |
Modules & ansible-doc |
| Task | one call of a module with arguments | Your First Playbook |
| Play | a list of tasks mapped to a group of hosts | |
| Playbook | a YAML file with one or more plays | |
| Handler | a task that runs only when notified, e.g. "reload nginx" | Handlers |
| Role | a reusable folder of tasks, templates, defaults and handlers | Roles |
| Collection | a distribution package of modules, plugins and roles | Collections & FQCNs |
Where Ansible sits
The devops-intro course separates three jobs:
| Job | Typical tool |
|---|---|
| create the infrastructure: networks, VMs, databases, DNS | Terraform / OpenTofu |
| configure what runs inside a server: users, packages, files, services | Ansible (this course), cloud-init for the first boot |
| package the application | Docker images, run on Kubernetes |
On a pure Kubernetes platform there is little left to configure on each node, so Ansible's share shrinks. It remains the standard tool for virtual machines and bare metal, for the nodes under a Kubernetes cluster, for network devices, for one-off fleet operations ("rotate this certificate on 300 servers") and for building golden images. Create with Terraform, Configure with Ansible puts the two together.
A first look
Ansible ships several commands. You will use these most:
| Command | Purpose |
|---|---|
ansible |
run one module against hosts (ad-hoc) |
ansible-playbook |
run a playbook |
ansible-inventory |
show what Ansible made of your inventory |
ansible-doc |
module documentation, offline |
ansible-config |
show settings and where they came from |
ansible-vault |
encrypt secrets |
ansible-galaxy |
install roles and collections, create role skeletons |
Here is a whole playbook run in this course's sandbox. It targets the machine itself (localhost), so it needs no SSH:
cat > hello.yml <<'YML'
- name: A first play
hosts: localhost
gather_facts: false
tasks:
- name: Create a config file
ansible.builtin.copy:
dest: app.conf
content: "port=8080\n"
mode: "0644"
YML
ansible-playbook hello.yml
echo "=== again"
ansible-playbook hello.yml | tail -2
PLAY [A first play] ************************************************************
TASK [Create a config file] ****************************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
=== again
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The first run reports changed=1: the file was created. The second run reports changed=0: the module compared the file with what you asked for and found nothing to do. That second line, repeated on every server, is what configuration management is about.
What you will build
The course follows the order you need in practice: inventories and ad-hoc commands, playbooks and modules, variables and templates, control flow, roles and collections, secrets with Vault, quality with ansible-lint and Molecule, and Ansible next to Terraform. The Mini Project turns the hardening steps of the linux-admin Mini Project into a role, tested with Molecule.
Test yourself
What must be installed on a server before Ansible can manage it?
- The Ansible agent
- Python and an SSH server (for Linux hosts)
- Docker
- Nothing at all, not even SSH
You run the same playbook twice without changing anything. What should the second PLAY RECAP show?
changed=0- The same
changedcount as the first run failed=1unreachable=1
Which split of work is typical?
- Ansible creates VPCs, Terraform edits /etc files
- Terraform creates the servers, Ansible configures what runs inside them
- Both do the same, pick one at random
- Ansible replaces Docker