You now have Ansible installed and the Aurora Store lab up and running. Before you run your first command, it's worth understanding how Ansible works under the hood: which pieces it is made of, how a task travels from your computer to web1, what runs there and how the result comes back. This knowledge isn't idle theory: it will help you interpret error messages, understand why a task reports changed or ok, and reason about performance and security. In this lesson you will walk through the components of the architecture, the life cycle of a task, idempotency in depth, parallelism and the command-line executables.
Contents
- Architecture overview
- Control node and managed nodes
- The inventory
- Modules
- Plugins
- The push model over SSH
- What happens when a task runs, step by step
- Idempotency in depth
- Parallelism: forks
- The Ansible executables
- Architecture overview
flowchart LR
subgraph CN["Control node (your computer)"]
direction TB
CLI["Executables<br/>ansible, ansible-playbook..."]
ENG["Ansible engine<br/>(ansible-core)"]
INV["Inventory<br/>web1, web2, db1"]
MOD["Modules<br/>(Python code)"]
PLG["Plugins<br/>connection, inventory,<br/>callback, become..."]
CLI --> ENG
INV --> ENG
MOD --> ENG
PLG --> ENG
end
ENG -- "SSH (push)" --> W1["web1<br/>Python 3"]
ENG -- "SSH (push)" --> W2["web2<br/>Python 3"]
ENG -- "SSH (push)" --> D1["db1<br/>Python 3"]
All of Ansible's "brains" live on the control node. The managed nodes only need SSH and Python: they don't run any permanent Ansible service.
| Component | Where it lives | Purpose |
|---|---|---|
| Executables (CLI) | Control node | Entry points: you invoke them |
| Engine | Control node | Reads the inventory and tasks, decides what to run, where and in what order |
| Inventory | Control node (files or dynamic sources) | Which machines exist and how they are grouped |
| Modules | Stored on the control node, executed on the managed nodes | Do the actual work |
| Plugins | Control node | Extend the engine: how to connect, how to display output, etc. |
- Control node and managed nodes
2.1 Control node
This is the machine where you installed Ansible in lesson 1.2 and where the aurora-infra/ repository lives. From it:
- The inventory, configuration and playbooks are read.
- SSH connections to the servers are opened.
- Results are received and displayed.
There can be several control nodes (each team member, a CI/CD server or a platform such as AWX, which you will see in Module 7), as long as they all use the same repository.
2.2 Managed nodes
These are web1, web2 and db1. Requirements (already checked in the previous lesson):
- A reachable SSH server.
- Python 3.
- A user to connect as (
vagrant) and, for administrative tasks, the ability to escalate privileges (sudo).
Ansible leaves nothing installed on them: after each task, it deletes any temporary files it used.
- The inventory
The inventory answers the question "which machines am I working on?". It contains:
- The hosts (names or IPs).
- The groups that organize them (
webservers,dbservers). - Optionally, associated variables (connection user, port, etc.).
For Aurora Store, the inventory will reflect this structure:
flowchart TD
ALL["all"] --> WS["webservers"]
ALL --> DS["dbservers"]
WS --> W1["web1"]
WS --> W2["web2"]
DS --> D1["db1"]
The all group always exists implicitly and contains every host. In the next lesson you will create a minimal inventory, and in Module 2 ("Inventory Files") you will learn its formats and capabilities.
- Modules
A module is a small program (almost always written in Python) that performs a specific action on the managed node and returns a result in JSON format. Examples:
| Module (FQCN) | What it does | Use at Aurora Store |
|---|---|---|
ansible.builtin.ping |
Checks that it can connect and run Python | Verify the lab |
ansible.builtin.apt |
Manages packages on Debian/Ubuntu | Install nginx and PostgreSQL |
ansible.builtin.copy |
Copies files to the server | Publish the website files |
ansible.builtin.service |
Manages services | Start nginx |
ansible.builtin.user |
Manages users | Create the deploy user |
The full name ansible.builtin.apt is called an FQCN (Fully Qualified Collection Name): ansible.builtin is the collection and apt is the module. You will study it in Module 2; for now, just remember that modules are the units of work and that each task invokes one.
- Plugins
While modules run on the managed nodes, plugins run on the control node and extend the engine's behavior. There are many types:
| Plugin type | Question it answers | Examples | Default |
|---|---|---|---|
| Connection | How do I connect to the host? | ssh, local, paramiko_ssh, winrm, community.docker.docker |
ssh |
| Become | How do I escalate privileges? | sudo, su, doas |
sudo |
| Inventory | Where do I get the list of hosts from? | ini, yaml, amazon.aws.aws_ec2 |
ini, yaml… |
| Callback | How do I display the results? | default, json, yaml (via format) |
default |
| Strategy | In what order do I run the tasks on the hosts? | linear, free |
linear |
| Lookup | How do I read external data? | file, env, password |
— |
| Filter | How do I transform data in templates? | upper, default, to_json |
— |
Some applied examples:
- To manage
web1, thesshconnection plugin is used, which invokes the system's OpenSSH client (that's why it honors your~/.ssh/config). - To install packages as root, the
sudobecome plugin is used. - The colors and formatting of the output you will see in your terminal are produced by the
defaultcallback plugin.
Plugins are covered in depth in Module 10; here it's enough to know they exist and what role they play.
- The push model over SSH
As you saw in the first lesson, Ansible works in push mode: you decide when changes are applied and the control node "pushes" them to the servers.
Practical implications for Aurora Store:
- Connectivity: your control node must be able to open SSH connections (port 22) to
web1,web2anddb1. The servers do not need to connect to you. - Security: SSH authentication is reused (keys,
known_hosts,sshdpolicies). There are no new ports or services. - When changes are applied: changes only happen when you run Ansible. If someone changes something by hand on
web1, it won't be corrected until the next run (which is why we will later automate periodic runs with AWX or CI/CD). - SSH performance: Ansible reuses connections through OpenSSH's ControlPersist feature, so it doesn't have to repeat the full SSH handshake for every task.
- What happens when a task runs, step by step
Imagine you ask Ansible to make sure nginx is installed on web1. This is what happens under the hood:
sequenceDiagram
participant U as You (terminal)
participant C as Control node<br/>(Ansible engine)
participant W as web1<br/>(managed node)
U->>C: Run task (apt module, name=nginx)
C->>C: Read inventory and configuration
C->>C: Package the module + arguments (AnsiballZ)
C->>W: 1. Open SSH connection (user vagrant)
C->>W: 2. Create temporary directory ~/.ansible/tmp/ansible-tmp-XXXX
C->>W: 3. Copy the module (AnsiballZ_apt.py) via SFTP/SCP
C->>W: 4. Run it with python3 (with sudo if become)
W->>W: The module compares current and desired state and acts if needed
W-->>C: 5. Return the result as JSON on standard output
C->>W: 6. Delete the temporary directory
C->>U: Display result (ok / changed / failed)
Let's go through each step:
-
Connection: the
sshconnection plugin opens (or reuses) an SSH connection to[email protected]. -
Temporary directory: a directory with a random name is created, for example
/home/vagrant/.ansible/tmp/ansible-tmp-1732615012.43-2741-90812/. -
Module transfer: Ansible doesn't send just the module file, but a self-contained bundle called AnsiballZ (
AnsiballZ_apt.py) that includes the module, the helper libraries it needs and the task's arguments. -
Execution: it runs with the server's Python interpreter (Ansible detects it automatically; on Ubuntu 24.04 it will be
/usr/bin/python3). If the task requires privileges, it is launched throughsudo. The module checks the current state (is nginx installed?) and only acts if it doesn't match the desired one. -
Result: the module prints a JSON object that the control node reads. Simplified example:
{ "changed": true, "cache_updated": false, "stdout": "Setting up nginx (1.24.0-2ubuntu7) ...", "invocation": { "module_args": { "name": "nginx", "state": "present" } } }changed: indicates whether the module modified anything on the server. It's the key to idempotency.- The remaining fields depend on the module and provide additional information.
- If something goes wrong, the JSON includes
"failed": trueand anmsgmessage explaining the error.
-
Cleanup: the temporary directory is removed. No trace of the module is left on the server.
There is an optimization called pipelining that skips steps 2, 3 and 6 by sending the module directly through Python's standard input. You will see it in the "Performance Tuning" lesson in Module 9.
If you run Ansible at the highest verbosity level (-vvv, which you will meet in the next lesson), you can literally see these steps in the output: the mkdir commands for the temporary directory, the sftp transfer and the final rm -rf.
- Idempotency in depth
8.1 Definition
An operation is idempotent if applying it once or N times leaves the system in the same end state, and runs after the first one make no changes.
A math example: multiplying by 1 or computing max(x, 5) are idempotent; adding 1 is not.
8.2 How a module achieves it
Every well-designed module follows a "check, then act" logic:
flowchart TD
A[Receive desired state<br/>nginx: present] --> B{Current state<br/>= desired?}
B -- Yes --> C[Do nothing<br/>changed: false → ok]
B -- No --> D[Apply the change<br/>install nginx]
D --> E[changed: true → changed]
For Aurora Store, running "nginx must be installed" twice on web1 would give:
| Run | State before | Module action | Displayed result |
|---|---|---|---|
| 1st | nginx not installed | Installs it | changed (yellow) |
| 2nd | nginx installed | Nothing | ok (green) |
| 3rd | nginx installed | Nothing | ok (green) |
8.3 Comparison with a script
Remember the manual procedure from the first lesson:
sudo ln -s /etc/nginx/sites-available/aurora /etc/nginx/sites-enabled/
echo "client_max_body_size 10m;" | sudo tee -a /etc/nginx/conf.d/aurora.conf- The first line fails the second time (the link already exists).
- The second line duplicates the directive every time it runs (
tee -aappends to the end).
Neither one is idempotent. The equivalent Ansible modules (ansible.builtin.file with state: link or ansible.builtin.lineinfile) check the state first and only act if necessary.
8.4 Why it matters so much
- Safe reruns: you can run Aurora Store's full automation whenever you want; only what has drifted will change.
- Drift correction: if someone modified something by hand, the next run brings it back to the defined state.
- Useful information: the
changedcounter tells you exactly what was modified. A second run withchanged=0is proof that your automation is idempotent.
8.5 Watch out: idempotency isn't always automatic
Some modules, such as ansible.builtin.command or ansible.builtin.shell, run arbitrary commands. Ansible can't tell whether an arbitrary command changed anything, so it always marks them as changed. That's why the general recommendation is: use the dedicated module whenever one exists and keep free-form commands as a last resort. You will see how to make these tasks idempotent in later modules.
- Parallelism: forks
Ansible doesn't manage servers one at a time: it opens several processes in parallel, called forks. By default it uses 5 forks, meaning it works on at most 5 hosts at once.
In addition, with the default strategy (linear), Ansible runs each task on every host before moving on to the next task:
gantt
title Linear strategy with forks = 5 (3 Aurora Store hosts)
dateFormat X
axisFormat %s
section web1
Task 1 :a1, 0, 3
Task 2 :a2, 4, 6
section web2
Task 1 :b1, 0, 2
Task 2 :b2, 4, 5
section db1
Task 1 :c1, 0, 4
Task 2 :c2, 4, 7
- Since there are only 3 hosts and 5 forks, all three run task 1 at the same time.
- Task 2 doesn't start until all of them have finished task 1 (
db1is the slowest and sets the pace).
What if Aurora Store had 20 web servers? With 5 forks, Ansible would process the task in batches of 5 simultaneous hosts. Increasing the number of forks speeds up runs on large fleets, at the cost of more CPU and memory usage on the control node. You will see how to configure it in the "Configuring Ansible (ansible.cfg)" lesson and how to fine-tune it in "Performance Tuning" (Module 9).
- The Ansible executables
Installing Ansible added several commands to your system. Each one has a specific purpose:
| Executable | Purpose | Example | Where it is covered |
|---|---|---|---|
ansible |
Run a module against a set of hosts (ad hoc commands) | ansible all -m ansible.builtin.ping |
Lesson 1.5 and Module 2 |
ansible-playbook |
Run playbooks (lists of tasks) | ansible-playbook site.yml |
Module 3 |
ansible-doc |
Look up module and plugin documentation without leaving the terminal | ansible-doc ansible.builtin.apt |
Lesson 1.5 |
ansible-inventory |
Display and validate the inventory as Ansible understands it | ansible-inventory --graph |
Lesson 1.5 and Module 2 |
ansible-config |
View and check the active configuration | ansible-config dump --only-changed |
Lesson 1.5 and Module 2 |
ansible-galaxy |
Install and create roles and collections | ansible-galaxy collection list |
Module 6 |
ansible-vault |
Encrypt files and variables containing secrets | ansible-vault create secrets.yml |
Module 5 |
ansible-console |
Interactive console for running modules (rarely used) | ansible-console webservers |
— |
ansible-pull |
Inverts the model: the node downloads and applies its own configuration (rarely used) | ansible-pull -U <repo> |
— |
Notice that ansible and ansible-playbook perform actions on the servers, while ansible-doc, ansible-inventory and ansible-config are query tools that don't touch the managed nodes.
Common Mistakes and Tips
- Thinking Ansible leaves an agent or a service on the servers: it doesn't; if you find
~/.ansible/tmpwith content on a server, it's usually leftovers from an interrupted run and can be deleted. - Confusing modules and plugins: modules run on the managed nodes and do the work; plugins run on the control node and extend the engine.
- Assuming
changedmeans "error":changedis normal the first time. What's suspicious is a task showing up aschangedon every run: it means it isn't idempotent. - Overusing
command/shell: you lose idempotency and reliable change reporting. - Python problems on the managed node: if a server doesn't have Python 3, almost every module will fail. This doesn't happen at Aurora Store because Ubuntu 24.04 includes it.
- Tip: when something fails and you don't understand why, remember the sequence diagram and ask yourself which step it fails at: the SSH connection? Permissions on the temporary directory?
sudo? The module itself?
Exercises
Exercise 1: Where does it run?
State whether each element runs on the control node or on the managed node: (a) the ssh connection plugin, (b) the ansible.builtin.apt module, (c) the callback plugin that colors the output, (d) reading the inventory.
Exercise 2: Idempotency
Classify each action as idempotent or non-idempotent, and explain why:
echo "10.0.0.5 db1" >> /etc/hosts- "The
postgresqlpackage must be present" (ansible.builtin.aptmodule). mkdir /srv/auroramkdir -p /srv/aurora
Exercise 3: Forks
Aurora Store grows to 12 web servers. With the default configuration, what is the maximum number of hosts that run a task simultaneously? In how many batches is each task processed? What would happen if you raised the forks to 15?
Solutions
Exercise 1. (a) Control node. (b) Managed node (it is copied over and run there with Python). (c) Control node. (d) Control node.
Exercise 2.
- Non-idempotent: each run appends one more line, duplicating the entry.
- Idempotent: the module checks whether the package is installed and only installs it if it's missing (
changedthe first time,okafterwards). - Non-idempotent: the second time it fails with "File exists".
- Idempotent in terms of state (it doesn't fail if the directory already exists), although as a command run with
ansible.builtin.command, Ansible would always mark it aschanged, because it can't tell whether it did anything. Theansible.builtin.filemodule withstate: directorywould be the right choice.
Exercise 3. With 5 forks, at most 5 hosts at a time; each task is processed in 3 batches (5 + 5 + 2). With 15 forks, all 12 servers would run each task simultaneously in a single batch, at the cost of more resource usage on the control node.
Conclusion
You now know Ansible's architecture: a control node with the engine, inventory, modules and plugins, which connects over SSH in push mode to the managed nodes, where it copies and runs Python modules that return JSON and disappear when they finish. You have seen why idempotency is Ansible's central property, how parallelism with forks speeds up the work, and what each executable is for. With this foundation, in the next lesson, Basic Ansible Commands, you will move on to practice: you will create a minimal inventory in aurora-infra/ and run your first commands against web1, web2 and db1.
Ansible Course: From Beginner to Advanced
Module 1: Introduction to Ansible
- What Is Ansible?
- Installing Ansible
- Setting Up the Practice Lab
- Ansible Architecture
- Basic Ansible Commands
Module 2: Ansible Fundamentals
- Inventory Files
- Inventory Variables: group_vars and host_vars
- Configuring Ansible (ansible.cfg)
- Ad Hoc Commands
- Ansible Modules
- Collections and Fully Qualified Names (FQCN)
Module 3: Playbooks
- Introduction to Playbooks
- Essential YAML for Ansible
- Writing Your First Playbook
- Playbook Structure
- Variables and Facts
- Conditionals and Loops
- Registering Results and Debugging Playbooks
Module 4: Roles
- Introduction to Roles
- Creating Roles
- Role Directory Structure
- Using Roles in Playbooks
- Reusing Tasks: include and import
- Role Dependencies and Argument Validation
Module 5: Advanced Playbook Techniques
- Handlers
- Jinja2 Templates
- Filters and Lookups
- Ansible Vault
- Error Handling
- Tags and Selective Execution
- Rolling Deployments and Delegation
- Optimizing Playbooks
Module 6: Ansible Galaxy
- What Is Ansible Galaxy?
- Using Ansible Galaxy
- Managing Dependencies with requirements.yml
- Creating and Sharing Roles
- Creating Your Own Collections
Module 7: Ansible Tower, AWX and Automation Platform
- Introduction to Ansible Tower and AWX
- Installing AWX
- Using AWX: Job Templates and Jobs
- Managing Projects and Inventories
- Credentials, Permissions (RBAC) and Workflows
- Execution Environments and ansible-navigator
Module 8: Ansible for DevOps
- Ansible and Docker
- Ansible and Kubernetes
- CI/CD with Ansible
- Ansible in Cloud Environments
- Ansible and Terraform
Module 9: Ansible Best Practices
- Code Organization
- Security Best Practices
- Performance Tuning
- Code Quality: ansible-lint and yamllint
- Testing Ansible Code with Molecule
Module 10: Advanced Ansible Topics
- Custom Modules
- Dynamic Inventory
- Ansible Plugins
- The Ansible API and ansible-runner
- Event-Driven Ansible
