Chapter 02 · Ansible Principles
Ansible Principles
Origins of Ansible
Ansible was created around a simple idea: automate without an agent, using existing protocols (SSH). The tool subsequently gained widespread adoption in businesses and the DevOps world.
Definitions and terminology
- Control node: the machine from which you run Ansible (often your workstation or a dedicated VM).
- Managed nodes: target machines (Linux, and Windows via WinRM).
- Inventory: a list of hosts and groups.
- Module: an action building block (package, file, service, user, etc.).
- Task: a call to a module with parameters.
- Playbook: a set of plays describing 'who' and 'what'.
- Role: reusable packaging (tasks/handlers/templates/defaults, etc.).
Ansible concepts
Declarative (as far as possible)
You describe the desired state. Modules do the work cleanly and idempotently.
Inventory + group_vars
You target groups of hosts and configure settings at different levels (global, group, host, role, etc.).
How does Ansible work?
- Ansible reads the inventory (hosts, groups and variables).
- It connects (SSH/WinRM), then copies and executes a module (often using Python on the target).
- It retrieves a JSON result and determines whether the state has changed.
Warning: on Linux, Ansible generally expects Python to be available on the target. Modern distributions usually include it, but not all do.
Administration: 'who is allowed to break what?'
- Privilege management through
become: true(sudo). - Secrets management through Ansible Vault (Chapter 8).
- Environment management using separate inventories (
dev,prod).
Practical exercise: first inventory + ping
- Create three VMs:
ctrl,node1,node2. - Configure SSH (generate the key on
ctrl, then copy it tonode1/node2). - On
ctrl, createinventory.ini:
[IP addresses]
ctrl = 192.168.1.142
node1 = 192.168.1.74
node2 = 192.168.1.21
[commands]
systemctl enable --now ssh (on node1 and node2)
ss -lntp | grep :22
ssh-keygen -t ed25519 -C "ansible@ctrl" (on ctrl)
ssh-copy-id patrick@192.168.1.74
ssh-copy-id patrick@192.168.1.21
and to test
ssh patrick@192.168.1.74
ssh patrick@192.168.1.21
[web]
node1 ansible_host=192.168.1.74
node2 ansible_host=192.168.1.21
[all:vars]
ansible_user=patrick
Then run:
ansible -i inventory.ini all -m ping
Expected result: a
pong on each node. If not: check SSH, the user, the key and the firewall. Yes, it is always SSH.Multiple-choice quiz
1) An Ansible module is:
- A. A CSS file
- B. An action building block (package, service, etc.)
- C. A hypervisor
2) What is the inventory used for?
- A. Listing hosts and groups
- B. Compiling the playbook
- C. Replacing DNS
3) By default, how does Ansible connect on Linux?
- A. FTP
- B. SSH
- C. Bluetooth
Answer key
1) B · 2) A · 3) B