How to Use Ansible: The Beginner's Guide (First Inventory, First Playbook, and the Idempotency Rule)

CodexFuse Team · 2026-10-30T13:33:55.533Z · 10 min read

If you manage more than one server and still configure each one by hand, learning how to use Ansible is one of the highest-leverage skills you can pick up. Ansible is an open-source automation tool that configures servers, deploys applications, and runs routine maintenance across your whole fleet from a single machine — with no software to install on the servers themselves. This beginner's guide walks you through the complete first-time workflow: installing Ansible on a control node, setting up SSH keys, writing your first inventory, running ad-hoc commands, and writing and safely testing your first playbook.

What Is Ansible? (A Plain-English Answer for Beginners)

Ansible is an agentless IT automation tool that lets you define how your servers should look — as plain YAML files called playbooks — and then applies that configuration to any number of machines over SSH. You install Ansible once, on one machine called the control node, and the servers you manage (managed nodes) need nothing except SSH access and Python. There are no agents to deploy, no daemons to keep alive, and no databases to maintain, which is why the official Ansible documentation describes it as automation "with no databases or daemons required" (docs.ansible.com, Installing Ansible).

The typical Ansible workflow for a beginner looks like this:

  1. Install Ansible on your control node — a Linux laptop, a Mac, or Windows under WSL.
  2. Set up SSH key access from the control node to every server you want to manage.
  3. Write an inventory file listing your servers, optionally in groups like webservers and dbservers.
  4. Test the connection with a one-line ad-hoc command such as ansible all -m ping.
  5. Write a playbook describing the desired state, preview it with --check --diff, then run it.

Once you see the shape of a finished playbook, you can also build one without writing YAML from scratch — the free Ansible playbook generator assembles a valid playbook task by task, which is handy while you are still learning the syntax.

What Do You Need Before You Install Ansible?

You need surprisingly little: one control node, one or more servers to manage, and SSH access between them. The control node — the machine where Ansible runs — can be nearly any UNIX-like machine with Python 3.9 or newer installed, including Debian, Ubuntu, Red Hat, macOS, and the BSDs. Windows is not natively supported as a control node; on Windows you must run Ansible inside WSL (Windows Subsystem for Linux), per the official installation docs (docs.ansible.com, Installing Ansible).

Your managed nodes are even simpler. They do not need Ansible installed at all — only SSH access and Python on the target, plus a user account that can reach an interactive shell. That agentless design is the whole point: you can start automating a server five minutes after it boots, before any configuration agent could be installed.

For your first lab, two small servers are enough. They can be cheap VPS instances — if the concept is new, our what is VPS hosting guide explains exactly what you are renting — or two virtual machines on your own hardware.

How Do You Install Ansible?

The officially supported install route is pip, and you have two packages to choose from: ansible (the "batteries included" community package) and ansible-core (the minimal language and runtime, without the community collections). Beginners should start with the full ansible package so every common module is available out of the box. The official docs give this exact command:

python3 -m pip install --user ansible

If you prefer the minimal footprint — for example on a CI runner — substitute ansible-core and add only the collections you need later. Here is how the three common install routes compare:

RouteCommandBest for
pip, community packagepython3 -m pip install --user ansibleBeginners on any Linux, macOS, or WSL — latest version with all community collections
pip, minimalpython3 -m pip install --user ansible-coreCI runners and containers where you want the smallest footprint
Distro package (Ubuntu)sudo apt install ansibleMachines where you want the OS package manager to handle updates (usually an older version)

Verify the install with ansible --version. If the shell cannot find the command after a --user pip install, add ~/.local/bin to your PATH.

How Do You Set Up SSH Keys for Ansible?

Ansible talks to your servers over SSH, and key-based authentication is the way to go — it is faster than typing passwords and required for any unattended run. Generate a key pair on your control node (skip this if you already have one you want to reuse):

ssh-keygen -t ed25519 -C "ansible-control"

Then copy the public key to each managed server with ssh-copy-id:

ssh-copy-id [email protected]
ssh-copy-id [email protected]

Replace deploy with your remote username and the hostnames with your servers' addresses. After this, ssh [email protected] should log you in without asking for a password. That passwordless hop is what makes every Ansible run after this one feel instant.

How Do You Write Your First Ansible Inventory?

An Ansible inventory is simply the list of servers Ansible manages, and grouping similar servers together is what makes playbooks powerful. The classic format is INI. Create a file called inventory.ini:

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com

Each [section] is a group, and you can target a whole group with one command or one playbook. Ansible also accepts YAML inventories, which some teams prefer for complex setups:

all:
  children:
    webservers:
      hosts:
        web1.example.com:
        web2.example.com:
    dbservers:
      hosts:
        db1.example.com:

You do not have to use the default location (/etc/ansible/hosts); passing -i inventory.ini on the command line, as every example in this guide does, keeps your lab self-contained and version-controllable.

How Do You Run Your First Ad-Hoc Command?

Ad-hoc commands are one-off Ansible commands — no playbook file needed — and they are the fastest way to prove your setup works. The classic first command pings every host in your inventory:

ansible all -i inventory.ini -m ping

The -m ping part selects the ping module (it tests the Ansible connection, not ICMP). A healthy reply looks like web1.example.com | SUCCESS => {"ping": "pong"}. Once that works, try something genuinely useful — checking uptime across the fleet in one shot:

ansible webservers -i inventory.ini -m ansible.builtin.command -a "uptime" --become

Notice the pattern every ad-hoc command follows: ansible [group] -i [inventory] -m [module] -a "[arguments]", with --become added when the task needs sudo. If you can ping and run commands, your control node is ready for real playbooks.

How Do You Write Your First Ansible Playbook?

An Ansible playbook is a YAML file that declares the desired state of your servers: which hosts, which tasks, in which order. Here is a complete first playbook — save it as site.yml — that installs nginx on your web servers and makes sure it is running:

---
- name: Configure web servers
  hosts: webservers
  become: true
  tasks:
    - name: Install nginx
      ansible.builtin.apt:
        name: nginx
        state: present
        update_cache: true

    - name: Ensure nginx is running and enabled
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: true

Read it top to bottom: the play targets the webservers group, escalates to root with become: true, then runs two tasks. Each task names a module (ansible.builtin.apt, ansible.builtin.service) and passes it parameters. There is no scripting logic — you describe the end state and Ansible figures out the steps. Run it with:

ansible-playbook -i inventory.ini site.yml

Before you trust any playbook you did not write yourself, paste it into the free Ansible playbook analyzer to review its plays, tasks, and modules at a glance — a quick structural sanity check costs nothing.

What Is Idempotency — Ansible's Golden Rule?

Ansible idempotency means you can run the same playbook ten times and get the same result as running it once: Ansible checks the current state first and only makes changes when reality differs from the desired state. Run the nginx playbook above a second time and you will see ok instead of changed on both tasks — nothing to do, so nothing done.

This is the property that makes Ansible safe for production. A shell script that runs apt install nginx blindly every time is just hoping for the best; an Ansible task declaring state: present is a guarantee. When you write your own tasks, always prefer modules that declare desired state (apt, copy, service, user) over raw shell or command calls, because shell commands are not idempotent by default — Ansible cannot know whether running them again is safe.

How Do You Test a Playbook Before It Touches Production?

You test with check mode — Ansible's dry run — combined with --diff to see exactly what would change. Make this a habit before every production run, not an afterthought:

ansible-playbook -i inventory.ini site.yml --check --diff

Check mode reports which tasks would change, without changing anything, and --diff shows the before-and-after for files and templates. It is not a perfect simulation — tasks that depend on earlier changes can misreport in check mode — but it catches the overwhelming majority of beginner mistakes: wrong paths, typos in package names, and plays aimed at the wrong group.

Two more safety nets belong in your muscle memory. First, ansible-playbook site.yml --syntax-check validates your YAML and playbook structure without connecting to anything. Second, start every new playbook against a single disposable host (or a --limit of one server) before you unleash it on the whole group.

How Do Ansible Variables and Handlers Work?

Ansible variables keep your playbooks reusable: instead of hardcoding values, you define them once per group or host and reference them with {{ }}. Create group_vars/webservers.yml next to your inventory:

http_port: 8080
max_clients: 200

Then use {{ http_port }} anywhere in the play. When the port changes, you edit one file instead of hunting through tasks. Variables can also live in the play itself under vars:, in host_vars/ per machine, or be passed on the command line with -e.

Ansible handlers are special tasks that run only when notified — the standard pattern is "restart the service, but only if its config actually changed":

    - name: Copy nginx config
      ansible.builtin.copy:
        src: nginx.conf
        dest: /etc/nginx/nginx.conf
      notify: Restart nginx

  handlers:
    - name: Restart nginx
      ansible.builtin.service:
        name: nginx
        state: restarted

If the config file is already identical, the task reports ok, no notification fires, and nginx keeps serving without a restart. That conditional restart is idempotency in action — and it is why handlers, not plain tasks, own every service restart in a well-written playbook.

These two features are what scale a playbook from "my two web servers" to real infrastructure: variables let one playbook serve staging and production, and handlers keep restarts surgical. The same patterns apply whether your targets are bare-metal boxes, Proxmox virtual machines from our Proxmox beginner guide, or hosts running Docker workloads from our Docker beginner guide.

Should You Use Ansible or Terraform?

This is the most common "which tool" question beginners ask, and the answer is that they do different jobs — most real infrastructures use both. Here is the honest comparison:

AnsibleTerraform
Primary jobConfigure what is inside servers: packages, users, config files, servicesProvision the infrastructure itself: VMs, networks, load balancers, DNS
LanguageYAML playbooksHCL (HashiCorp Configuration Language)
How it connectsAgentless — SSH or WinRM, nothing installed on targetsCloud/provider APIs — talks to AWS, Azure, Proxmox, and others
StateStateless by design — re-checks reality on every runKeeps a state file tracking what it created
Typical orderRuns after servers existRuns first to create the servers
Learn it whenYou have servers and configure them by handYou create infrastructure by clicking in a console

The one-line version: Terraform builds the servers, Ansible configures them. If your pain is "I SSH into five machines and run the same commands," that is an Ansible problem — start here.

What Mistakes Do Ansible Beginners Make?

Almost every beginner failure falls into one of five buckets, and all five are avoidable. First, YAML indentation errors — Ansible playbooks are whitespace-sensitive, so use spaces (never tabs), keep list items aligned, and run --syntax-check early and often. Second, forgetting become: true — package installs and service management need root, and a play without privilege escalation fails with permission errors that look more mysterious than they are.

Third, running untested playbooks on production — the --check --diff habit from the section above exists precisely for this. Fourth, trying to run the control node on native Windows — it is not supported; use WSL and save yourself the grief (Windows targets, on the other hand, work fine over WinRM). Fifth, hardcoding secrets in playbooks — passwords and API tokens belong in Ansible Vault encrypted files, never in plaintext YAML committed to git.

Sources

  • Ansible Documentation — "Installing Ansible" (docs.ansible.com/projects/ansible/7/installation_guide/intro_installation.html) — control node requirements (Python 3.9+, UNIX-like, Windows only under WSL), managed node requirements (SSH + Python, no Ansible install), ansible vs ansible-core packages, python3 -m pip install --user ansible. Fetched 2026-10-30.
  • DevOpsBoys — "How to Set Up Ansible from Scratch (Complete Beginner Guide 2026)" (devopsboys.com/blog/how-to-set-up-ansible-from-scratch-2026) — 2026 tutorial structure: install, SSH keys, inventory, first playbook, variables and loops, when to use Ansible vs Terraform. Fetched 2026-10-30.
  • HowToUseLinux — "Getting Started with Ansible: A Simple Guide for Beginners" (howtouselinux.com/post/getting-started-with-ansible-a-simple-guide-for-beginners) — beginner workflow: install, inventory, playbook, --check tip. Fetched 2026-10-30.
  • Cylab — "Getting started with Ansible" (cylab.be/blog/225/getting-started-with-ansible) — control node/managed node setup, inventory groups, handlers documentation references. Fetched 2026-10-30.

Frequently Asked Questions

Can you run Ansible on Windows?

As a control node — the machine that runs Ansible — no, not natively: you must use Windows Subsystem for Linux (WSL), per the official docs. As a managed node — a server Ansible configures — yes: Windows targets are supported over WinRM instead of SSH.

Do you need to install Ansible on the servers you manage?

No. Ansible is agentless: only the control node needs Ansible installed. Managed nodes just need SSH access, Python, and a user account with an interactive shell.

Ansible vs Terraform — which should I learn first?

Learn the one that matches your pain. If you create servers by clicking in a cloud console, learn Terraform first. If you already have servers and configure each one by hand over SSH, learn Ansible first — most teams end up using both.

How long does it take to learn Ansible basics?

A focused weekend is enough for the core loop: install the control node, set up SSH keys, write an inventory, run ad-hoc commands, and write and test a first playbook. Variables, handlers, and roles come naturally over the following weeks as your playbooks grow.

Is Ansible free to use?

Yes. Ansible and ansible-core are open-source software, free to download and use without limits. Red Hat also sells Ansible Automation Platform, a paid product with a web UI and support, but the command-line Ansible this guide teaches costs nothing.

Featured on Twelve Tools Launch Llama Tools Launch your MCP