Expert · Ansible fundamentals · topic 44 of 57 · 30 min
What Ansible is and how it works
Explanation
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.
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
| Name | What it is |
|---|---|
| ansible-core | The engine and command-line tools (ansible, ansible-playbook, ansible-doc, ansible-galaxy) plus the ansible.builtin modules. In RHEL 9 AppStream. |
| Collections | Packages of extra modules and roles, such as ansible.posix or community.general. |
| ansible-navigator | A 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 Platform | Red Hat's supported product: a web UI and API (automation controller), certified content, execution environments. |
# 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']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:
- Indentation shows structure, and it must be SPACES (usually 2), never tabs.
- key: value pairs, with a space after the colon.
- A list item starts with "- " (dash and space).
- Quote values that start with {{ (Jinja) or contain ": " or #.
- A file may start with --- .
---
server:
name: web01
ports:
- 80
- 443
owner: "Asha Rao"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
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.
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=0Practice (0/3 done)
What do managed nodes need for Ansible to work?
You run the same playbook twice on a correct server. What should the second run report?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Inventory and ansible.cfgAnsible fundamentals · same module
- Ad-hoc commands and ansible-docAnsible fundamentals · same module
- Writing and running playbooksAnsible fundamentals · same module
- Ansible: inventory and ad-hoc commandsDevOps: Git, Docker, Kubernetes and CI/CD · Configuration management with Ansible, and YAML
- Configuration management: Ansible, Puppet, Chef and NETCONFComputer Networking (CCNA level) · Automation, SDN and cloud networking
- RHEL System Roles and Ansible VaultAnsible in depth
Questions people ask about this
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.
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.
What does -C do with ansible-playbook?Related
Answer: Check mode: predict changes without making them
It is a dry run.
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
- 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.
- Module 9 exam: Ansible fundamentals · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- 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