How Ansible Works
Ansible runs on one machine, the control node, which can be your own computer. It connects to the managed nodes over SSH, copies small programs there, runs them, and removes them. There is no agent or background service on the servers, and nothing listening on an extra port.
| Term | Meaning |
|---|---|
| Inventory | The list of servers |
| Module | One unit of work |
| Task | A module with settings |
| Playbook | Tasks in order, in YAML |
| Role | A reusable set of tasks |
Install Ansible
Install it on the control node only. On Ubuntu and Debian:
sudo apt install ansible
On AlmaLinux and Rocky Linux, the AppStream repository carries ansible-core. On release 9 the full ansible package with the community collections comes from EPEL; EPEL 10 offers only separate collection packages, so use pipx below for the full set:
sudo dnf install ansible-core
For a newer version than your distribution ships, install it for your user with pipx:
pipx install --include-deps ansible
Check with ansible --version.
Prepare SSH Access
Ansible logs in like you do. Set up key login to each server first (see how to generate SSH keys), and make sure a plain ssh to each one works. Host aliases, ports, and keys in your SSH config file are used by Ansible too.
Write an Inventory
Create inventory.ini:
[web]
web1 ansible_host=203.0.113.10 ansible_user=deploy
[db]
db1 ansible_host=203.0.113.11 ansible_user=deploy
Groups in brackets let you target several servers at once.
Test the Connection
ansible -i inventory.ini all -m ping
This is an ad-hoc command: one module run against a group without a playbook. The ping module is not an ICMP ping; it logs in over SSH and checks that Python works. Each host should answer with pong.
A First Playbook
Create site.yml:
- name: Web servers
hosts: web
become: true
tasks:
- name: Install Nginx
ansible.builtin.package:
name: nginx
state: present
- name: Start Nginx at boot
ansible.builtin.service:
name: nginx
state: started
enabled: true
become: true runs the tasks with sudo. If your user needs a password for sudo, add --ask-become-pass when you run it.
Run It Safely
ansible-playbook -i inventory.ini site.yml --check --diff
ansible-playbook -i inventory.ini site.yml
--check reports what would change without changing anything, and --diff shows the changed lines of files. Not every module can predict its result in check mode, but it catches most mistakes. Try a new playbook on one server first with --limit web1.
Running Twice Changes Nothing
Run the playbook a second time. The tasks now report ok instead of changed, because the package is already installed and the service already runs. Modules check the current state before acting. This is what makes it safe to run the same playbook again and again, and a task that reports changed on every run is a sign it needs fixing.
Secrets, Roles, and Collections
- Ansible Vault:
ansible-vault create secrets.ymlkeeps passwords encrypted in the same Git repository. - Roles: group the tasks, templates, and variables for one job, such as "web server", so playbooks stay short.
- Collections: bundles of modules and roles, installed with
ansible-galaxy collection install.
Ansible configures servers that exist. Creating them is the strength of Terraform; the split is explained in Ansible vs Terraform.