Ansible
Ansible is an automation tool that configures computers over SSH.
Instead of manually typing commands on every machine, you describe the desired state in a YAML file called a playbook, and Ansible makes the machine match that state.
In this section, Ansible is also used in a different way: it configures a mounted Raspberry Pi image through a chroot connection, not through SSH.
Install
Ansible playbook in nutshell
A playbook is a YAML file that tells Ansible what state a host should have.
Basic idea:
hosts: which machines from the inventory to configurebecome: run tasks with sudotasks: ordered steps to apply- each task usually calls one Ansible module
Good playbooks are usually idempotent: running them again should not break the machine or repeat work that is already done.
| simple_playbook.yml | |
|---|---|
Run it:
Common task examples
Install packages:
Copy a file:
Create a directory:
Run a command:
Manage a service:
Most used tasks
| Goal | Module | Short example |
|---|---|---|
| Install package | ansible.builtin.apt |
name: nginx, state: present |
| Copy file | ansible.builtin.copy |
src: app.conf, dest: /etc/app.conf |
| Create directory | ansible.builtin.file |
path: /opt/app, state: directory |
| Run command | ansible.builtin.command |
cmd: uname -r |
| Manage service | ansible.builtin.service |
name: nginx, state: started |
| Template config | ansible.builtin.template |
src: app.conf.j2, dest: /etc/app.conf |