Ansible for beginners
In short: Ansible sets up servers automatically. In a text file you describe the wanted state (“package nginx installed, service running”) and Ansible creates it over SSH without installing a program on the target machine.
Building blocks
- Inventory: list of machines (
hosts.ini). - Playbook: YAML file with tasks.
- Modules: ready-made blocks such as
apt,copy,service,user.
Example
- name: Set up web server
hosts: webserver
become: true
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
- name: Start nginx
ansible.builtin.service:
name: nginx
state: started
enabled: trueStart: ansible-playbook -i hosts.ini playbook.yml. Test it safely first with --check.
Idempotence
A playbook may run any number of times and changes only what is necessary. On the second run everything says “ok” instead of “changed”. That is why you use modules (“create state”) instead of shell commands (“do something”).
When is it worth it?
From the second server on, or if you want to rebuild a machine reproducibly. For a single private computer dotfiles and a script are enough. The file format is explained in File formats for configuration and data.
See also: SSH on Linux