Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 29 ยท Getting Started

What Is Ansible?

Learn what Ansible is, how its agentless push model works over SSH, why idempotence and desired state matter, and when to pick it over shell scripts.

Ansible is a tool for describing how your servers should look and then making them look that way. You write a short text file that says "this package is installed, this file has this content, this service is running", and Ansible connects to each machine and does only the work that is still needed. It is used for installing software, writing configuration files, creating users, deploying applications and a hundred other jobs that sysadmins used to do by hand, one SSH session at a time.

You could do all of that with shell scripts, and many teams start there. The trouble appears when you have twenty servers instead of one, when a script half-fails and you must run it again, or when two servers have drifted apart and nobody knows how. Ansible was built for exactly those situations. This lesson explains the ideas behind it, so that the commands in the next lessons make sense.

Configuration management in one sentence

Configuration management means keeping the setup of many machines written down, repeatable and checkable. Instead of "I logged in and ran some commands last Tuesday", you have a file in Git that anyone can read, review and run again.

Compare the two styles:

Terminal
# A script: a list of steps to perform
mkdir /srv/app
echo "Managed by script" >> /etc/motd
apt-get install -y nginx
systemctl start nginx
YAML
# Ansible: a description of the end result
- name: Make sure the app directory exists
  ansible.builtin.file:
    path: /srv/app
    state: directory

The script is a recipe of actions. Run it twice and mkdir fails on the second run, and the echo adds a second line. The Ansible task is a statement about the result: "/srv/app is a directory". If it already is, Ansible does nothing and reports ok. If not, it creates it and reports changed.

You will see real Ansible commands and YAML in read-only code blocks in this tutorial, because the site cannot connect to servers for you. The runnable TryIt examples are small JavaScript stand-ins that copy the idea so you can experiment. Each lesson says which is which.

The agentless push model

Many management tools (Puppet and Chef in their classic form, for example) install an agent, a background program on every managed machine, which pulls instructions from a central server. Ansible does not. It works like this:

flowchart LR
    C["Control node<br/>(your laptop or CI runner)<br/>ansible installed here"] -->|"SSH"| W1["web1<br/>managed node"]
    C -->|"SSH"| W2["web2<br/>managed node"]
    C -->|"SSH"| D1["db1<br/>managed node"]

The control node connects to every managed node over SSH, runs the work, and disconnects.

  • The control node is the machine where you run ansible and ansible-playbook. It needs Ansible and Python installed. It can be a Linux or macOS machine; on Windows you use WSL (covered in the next lesson).
  • The managed nodes are the machines you configure. They need an SSH server and Python (for most modules), and nothing else. There is no Ansible agent, no database and no open extra port.
  • It is a push model: you decide when to run, and Ansible pushes the work out. Nothing happens on the servers between runs.

For each task, Ansible copies a small Python program (a module) to the managed node, runs it, reads the JSON result and deletes the program again. The module does the real work on the target, such as installing a package or editing a file. The control node only coordinates.

Agentless has real advantages: nothing to install or upgrade on servers, no extra service that can break or be attacked, and a new server is manageable the moment you can SSH into it. The cost is that runs are driven from the control node, so very large fleets need extra tooling (see Performance, Dynamic Inventory and AWX).

Desired state and idempotence

Two words describe why Ansible feels different from a script.

Desired state means each task describes how things should be, not what to type. state: present, state: started and state: absent appear in almost every module.

Idempotence means running the same task many times gives the same result as running it once. The first run changes the machine. The second run finds nothing to do. This gives you three things at once:

  1. You can re-run safely. After a failed run you fix the problem and run again. Tasks that already succeeded are skipped quickly.
  2. You can detect drift. If someone edited a file by hand, the next run notices and fixes it, and reports changed so you see that it happened.
  3. You can trust the report. A run that ends with changed=0 tells you that the machines already matched the description.

Under the hood, every well-written module follows the same loop: look at the current state, compare it with the wanted state, change only if they differ.

flowchart TD
    A["Read current state"] --> B{"Same as<br/>desired state?"}
    B -- yes --> C["Report ok<br/>(change nothing)"]
    B -- no --> D["Change the machine"]
    D --> E["Report changed"]

Every idempotent module follows this check-then-change loop.

The stand-in below is a pretend host made of directories and file lines. The two helper functions check first and change only when needed, just like Ansible modules. Run it and compare the first run with the later ones.

JS Check state, then change (idempotence)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first run reports two changes. Runs two and three report only ok, and the file still contains Managed by Ansible exactly once. This is the behavior real modules such as ansible.builtin.file and ansible.builtin.lineinfile give you. The stand-in has no SSH and no Python modules, of course; it only mirrors the logic.

What a script does instead

Now look at the same job written as a plain "do this" script. Each run repeats the action blindly.

JS A naive script versus desired state

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The script grows the file on every run. The desired-state version stops at two lines. Real Ansible lets you write shell commands too (the command and shell modules), but then you must make them idempotent. That topic gets its own lesson: Modules and Idempotence.

Where Ansible fits

Ansible is a good choice when:

  • you manage several machines that should be configured the same way, or one machine you want to rebuild reliably;
  • you want setup instructions in version control, reviewed like code;
  • you cannot or do not want to install agents everywhere;
  • the work is mostly configuration and orchestration: packages, files, users, services, rolling deployments, cloud and network devices.

It is not the best tool when:

  • you need to create cloud infrastructure from scratch with state tracking and destroy plans (Terraform does that better; many teams use both together, see Ansible with Docker, Terraform and CI/CD);
  • you need tight per-second reaction on thousands of nodes (an agent-based or Kubernetes-native system fits better);
  • the job is a single throwaway command on one server (just use SSH).

Because Ansible uses YAML, there is no programming language to learn first. You still need basic Linux skills: if SSH, users, permissions and systemctl are new to you, the Linux tutorial helps a lot.

Your first look at the vocabulary

You will meet these words in every lesson:

Term Meaning
Control node The machine you run Ansible from
Managed node (host) A machine Ansible configures
Inventory The list of hosts and groups
Module A small program that does one job (install a package, copy a file)
Task One call of a module with arguments
Play A set of tasks applied to a group of hosts
Playbook A YAML file containing one or more plays
Role A reusable bundle of tasks, files and templates
Handler A task that runs only when notified, such as "restart nginx"
Fact A piece of information Ansible gathers about a host

Do not memorize the table. Each term is introduced properly when you need it, starting with the inventory.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement best describes Ansible's agentless design?

    Ansible pushes work over SSH (WinRM for Windows). Managed nodes need an SSH server and usually Python, but no Ansible agent or daemon.
  2. 2. A playbook run finishes with changed=0 on every host. What does that tell you?

    Idempotent tasks report ok when the system is already correct. changed=0 together with failed=0 and unreachable=0 means the machines already matched the description.
  3. 3. What does idempotence mean for an Ansible task?

    An idempotent task checks the current state and only acts when it differs from the wanted state, so repeated runs are safe.
  4. 4. Which of these is the best fit for Ansible?

    Repeatable, reviewable configuration of several machines is Ansible's core use case. A single one-off command is easier with plain SSH.

Summary

  • Ansible describes the desired state of servers in YAML and makes machines match it.
  • It is agentless: a control node pushes small Python modules over SSH to managed nodes and removes them afterwards.
  • Idempotent modules check the current state first and change only what differs, reporting ok or changed.
  • Idempotence makes re-runs safe, exposes drift, and makes the final report trustworthy.
  • Ansible shines for configuration and orchestration; use other tools for provisioning cloud infrastructure from scratch or for agent-style continuous enforcement.
  • Plain shell scripts repeat their actions on every run unless you build the checks yourself.

Next: Installing Ansible and Your First Ping