Skip to content

Expert · Ansible fundamentals · topic 44 of 57 · 30 min

What Ansible is and how it works

Explanation

Ansible is an automation tool: you describe the state you want (packages installed, services running, files in place) and it makes servers match. It is agentless: a control node connects to managed nodes over SSH and runs small programs called modules. Good modules are idempotent: running them twice changes nothing the second time.

Why automate?

Setting up one server by hand takes an hour. Setting up fifty takes a week, and no two end up exactly the same: someone forgets a firewall rule here, a package version differs there. These small differences ("configuration drift") cause the hardest outages. Automation writes the steps down once, as code, and applies them the same way everywhere. The code also documents the setup and can be reviewed and versioned in Git.

How Ansible works

  • Control node: the machine where Ansible is installed and where you run it (your admin workstation or a jump server). Needs Linux and Python.
  • Managed nodes: the servers you configure. They need only SSH and Python, no Ansible agent. That is what "agentless" means.
  • Inventory: a list of managed nodes, in groups such as webservers and dbservers.
  • Module: a small program for one job (install a package, copy a file, create a user). Ansible copies it to the node over SSH, runs it, and reads back a JSON result.
  • Playbook: a YAML file listing which modules to run on which hosts, in order.
Think of it like this: Ansible is like a head chef with a written recipe card for every dish. The chef (control node) does not stand in every branch kitchen (managed nodes); she phones each one (SSH) and reads out exact steps. Because the steps say "make sure there are 2 kg of rice" and not "add 2 kg of rice", reading the card twice does not double the rice. That is idempotency.

Idempotency: the key idea

Shell scripts usually say what to DO: "append this line", "create this user". Run them twice and you get two lines, or an error that the user exists. Ansible modules say what should BE TRUE: "this line is present", "this user exists". The module checks first and only changes something if needed. The result says "ok" (already right) or "changed" (fixed it). So you can run the same playbook every day safely; it fixes drift and leaves correct servers alone.

Ansible products

NameWhat it is
ansible-coreThe engine and command-line tools (ansible, ansible-playbook, ansible-doc, ansible-galaxy) plus the ansible.builtin modules. In RHEL 9 AppStream.
CollectionsPackages of extra modules and roles, such as ansible.posix or community.general.
ansible-navigatorA newer text interface that runs playbooks inside an execution environment (a container image with Ansible and collections). Part of Red Hat Ansible Automation Platform.
Ansible Automation PlatformRed Hat's supported product: a web UI and API (automation controller), certified content, execution environments.
bash
# dnf install -y ansible-core
$ ansible --version | head -3
ansible [core 2.14.17]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/asha/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
The exact version depends on your RHEL minor release.

YAML in five rules

Playbooks and many inventories are written in YAML, a text format for data. Learn these rules now; most beginner errors are YAML errors:

  1. Indentation shows structure, and it must be SPACES (usually 2), never tabs.
  2. key: value pairs, with a space after the colon.
  3. A list item starts with "- " (dash and space).
  4. Quote values that start with {{ (Jinja) or contain ": " or #.
  5. A file may start with --- .
yaml
---
server:
  name: web01
  ports:
    - 80
    - 443
  owner: "Asha Rao"
A dictionary (server) holding a string, a list and another string.

Common mistakes

  • Using Ansible like a shell script: running shell or command modules for everything. Prefer a real module; it is idempotent and reports changes correctly.
  • Tabs in YAML. Configure your editor to insert spaces.
  • Forgetting that every managed node needs Python. RHEL 9 has python3 by default; minimal containers may not.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

What Ansible is and how it works · 1/6

Describe the state → Ansible applies it

Ansible makes servers match a state you describe, instead of you typing the steps on each one.

Prefer a video by a teacher? Find free videos on “ansible tutorial for beginners how ansible works agentless” on YouTube ↗

In real life: Patching 300 branch servers at a bank

A bank's IT team in Chennai manages about 300 RHEL servers in branches across Tamil Nadu. Applying a security fix by hand took two engineers a full week. With Ansible, one playbook updates the package, restarts the service only where it changed, and prints a summary: which servers changed, which were already fine, and which were unreachable for follow-up. The job now takes an afternoon.

bash
PLAY RECAP *********************************************************
branch-che-001 : ok=4  changed=2  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
branch-che-002 : ok=4  changed=0  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
branch-mdu-014 : ok=0  changed=0  unreachable=1  failed=0  skipped=0  rescued=0  ignored=0

Practice (0/3 done)

Task 1 · Question

What do managed nodes need for Ansible to work?

Task 2 · Question

You run the same playbook twice on a correct server. What should the second run report?

Task 3 · Code · Python

Show idempotency in Python. Write ensure_line(lines, line) that returns a tuple (new_lines, changed). If line is already in the list, return the same list and False. Otherwise return a NEW list with line added at the end, and True. Do not change the list you were given. (This is what ansible.builtin.lineinfile does.)

Quick quiz

Question 1

1.What does "agentless" mean for Ansible?

Question 2

2.Which package gives you ansible-playbook on RHEL 9?

Question 3

3.Which is NOT allowed in YAML indentation?

Question 4

4.What is an execution environment?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What does "agentless" mean for Ansible?

Answer: No Ansible software has to run permanently on managed nodes

It connects over SSH and runs modules on demand.

Which package gives you ansible-playbook on RHEL 9?

Answer: ansible-core

ansible-core.

Which is NOT allowed in YAML indentation?

Answer: Tabs

YAML forbids tabs for indentation.

What is an execution environment?

Answer: A container image with Ansible and collections, used by ansible-navigator

Navigator runs playbooks inside it.

Why prefer ansible.builtin.apt over ansible.builtin.shell with "apt-get install -y nginx"?Related

Answer: apt is idempotent and reports changed only when it really changes something

shell runs every time and always reports changed; the apt module checks state first.

From Ansible: inventory and ad-hoc commands

In ansible.cfg, what does become = True do?Related

Answer: Uses privilege escalation (sudo by default) on managed nodes

Tasks run as become_user (root) via sudo.

From Inventory and ansible.cfg

What does -C do with ansible-playbook?Related

Answer: Check mode: predict changes without making them

It is a dry run.

From Writing and running playbooks

Which TCP port does NETCONF over SSH use?Related

Answer: 830

NETCONF over SSH uses TCP port 830. RESTCONF runs over HTTPS.

From Configuration management: Ansible, Puppet, Chef and NETCONF

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 4 more topics in “Ansible fundamentals” before its exam: Inventory and ansible.cfg, Ad-hoc commands and ansible-doc, Writing and running playbooks, Everyday modules and handlers.
  2. Module 9 exam: Ansible fundamentals · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Ansible in depth. 57 topics left in the course before the final exam.

← Previous: Persistent storage and containers as systemd services