Ansible tutorial for beginners

Ansible Tutorial for Beginners: Write Your First Playbook

Introduction

Ansible configures servers that already exist — a fleet of machines that all need the same software, configuration files and services. Ansible lets you describe the end state once, in a readable YAML file, and apply it to one server or a thousand with a single command.

This tutorial teaches Ansible from zero: install it, talk to your servers, and write a playbook that installs and starts a real web server. You need the terminal and basic Linux Commands for DevOps Engineers. Playbooks are YAML, not code.

Version note (27 Nov 2026): the latest ansible-core is 2.22.0, released 2 November 2026 (the community ansible package is at 15.0.0, released 17 November 2026, built on ansible-core 2.22.0). Ansible-core 2.19 reaches end of life on 30 November 2026.

What Ansible Is and When to Use It

Ansible is an open-source automation engine for configuration management, application deployment and orchestration. You describe the desired state of your machines — “nginx installed, running, and serving this page” — and Ansible makes it so; run the same playbook again and nothing changes. That property is called idempotency.

Three design choices make Ansible easy to learn. First, it is agentless: install Ansible on one machine, the control node (your laptop is fine), and it manages other machines over SSH — no agent, no database, no daemons. Second, it is push-based: the control node connects out, does the work, and disconnects. Third, everything is YAML: playbooks, inventory and variables are plain text you can version in Git.

Ansible copies small programs called modules to the managed node and runs them there. A module does one job: install a package, copy a file, start a service, create a user. A task calls one module, a play maps tasks onto a group of hosts, and a playbook is a list of plays — the “configure” half of infrastructure as code (see our DevOps Engineer Roadmap 2026).

Installing Ansible

Install Ansible on your control node only — the machines you manage do not need it. There are two packages: ansible-core, the minimal engine (language, runtime, built-in modules), and ansible, the larger community package (ansible-core plus a curated bundle of collections). For learning, install the full ansible package.

Install with pip, the officially supported method, inside a virtual environment:

python3 -m venv ~/ansible-venv
source ~/ansible-venv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install ansible

Check it with ansible –version: expect ansible-core 2.22.0 (or later). One gotcha: ansible-core 2.22 needs Python 3.13 or newer on the control node, and managed nodes need Python 3.9 to 3.15. If your system’s Python is older, pip will install an older ansible-core instead, so check the version. Windows is not supported as a control node; use Linux, macOS, or WSL.

Inventory and Ad-Hoc Commands

Ansible needs to know which machines to manage — that list is the inventory. Create inventory.ini:

[webservers]
web1.example.com ansible_host=203.0.113.10
web2.example.com ansible_host=203.0.113.11

[webservers:vars]
ansible_user=ubuntu

Each host sits under a group — here, webservers — with ansible_host giving its address, and [webservers:vars] setting the ubuntu user for the whole group. (The IPs are examples — replace them with your own servers.)

Before writing a playbook, test the connection with an ad-hoc command — a single task from the command line, with no file involved. The ping module checks that Ansible can log in over SSH and run Python on the host (it is not an ICMP ping):

ansible all -i inventory.ini -m ping

Expect SUCCESS and “ping”: “pong” per host — if not, check the SSH key, user, or address. Fix the connection now: every playbook later depends on it.

Ad-hoc commands are also useful day to day — run a command on every host, or install a package on one group with become (sudo):

ansible all -i inventory.ini -a "uptime"
ansible webservers -i inventory.ini -m ansible.builtin.apt -a "name=curl state=present" --become

The -m flag names the module, -a passes its arguments, and the module name is written in full — ansible.builtin.apt — the fully qualified collection name (FQCN), recommended since version 2.10.

Your First Playbook

A playbook is a YAML file of plays, each listing tasks. Here is a complete, runnable one that installs nginx, deploys a web page, and starts the service — save it as webserver.yml:

---
- name: Install and start a web server
  hosts: webservers
  become: true
  tasks:
    - name: Install nginx
      ansible.builtin.apt:
        name: nginx
        state: present
        update_cache: true

    - name: Deploy a test web page
      ansible.builtin.copy:
        dest: /var/www/html/index.html
        content: |
          <h1>Hello from Ansible</h1>
          <p>This page was deployed by Ansible.</p>
        mode: "0644"

    - name: Start nginx and enable it at boot
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: true

The play targets the webservers group, and become: true means every task runs with sudo. Each task has a human-readable name, printed as Ansible runs. The apt module installs the package only if it is missing, copy writes the file only if its content differs, and service starts nginx only if it is not already running. That is idempotency: run this playbook five times and only the first run changes anything.

Before running it for real, do a dry run: –check predicts what would change without changing anything, and –diff shows the exact file changes:

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

The output should show nginx installed, the page created, the service started — nothing else. If it does, run it for real with ansible-playbook -i inventory.ini webserver.yml. Check the server’s address in a browser or with curl: you should see “Hello from Ansible”. Run the playbook a second time — everything reports ok, nothing changed. That second run is the proof of idempotency. On RHEL, Rocky or Fedora, use ansible.builtin.dnf instead of ansible.builtin.apt.

Variables, Handlers and Templates

Hard-coded values do not survive a second environment, so the port number and page message should be variables: the same playbook then serves staging and production. Group variables live in group_vars/webservers.yml, and Ansible loads them automatically when the group name matches the file name:

---
http_port: 80
site_message: "Hello from Ansible"

Use them with Jinja2 {{ }} syntax; the template module renders the file on the control node and copies the result to the host:

    - name: Deploy the web page from a template
      ansible.builtin.template:
        src: index.html.j2
        dest: /var/www/html/index.html
        mode: "0644"
      notify: Restart nginx

Create index.html.j2:

<h1>{{ site_message }}</h1>
<p>Served by {{ ansible_hostname }} on port {{ http_port }}.</p>

ansible_hostname is a fact — information Ansible gathers about each host at the start of a play, such as its hostname and operating system. Facts are why a template can differ on every server.

The notify: Restart nginx line introduces handlers: tasks that run only when notified, once per play, at the end. Define it at the end of the play:

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

Handlers restart a service only when its configuration changed — without them, every run would restart nginx.

Roles, Vault and Good Practice

A playbook that installs one web server is a good start; one that also installs a database and a monitoring agent is a long file, and long files hide mistakes. Roles are how Ansible organises that growth: a role bundles the tasks, handlers, variables, templates and files for one job — “the web server” — so a playbook becomes a short list of roles. Scaffold one with a single command:

ansible-galaxy init roles/webserver

That creates the standard layout — tasks/, handlers/, defaults/, vars/, files/, templates/, meta/, each with a main.yml. Move the nginx tasks, handler and template into place, and the playbook shrinks to this:

---
- name: Configure web servers
  hosts: webservers
  become: true
  roles:
    - webserver

Roles are also how you reuse work: Ansible Galaxy (galaxy.ansible.com) hosts thousands of community roles — check there before writing one for a common tool. ansible-galaxy role install geerlingguy.nginx installs a popular nginx role in seconds; vet it like any dependency.

Never put a password, API key or private key in a playbook or variables file in plain text — those files live in Git. Ansible Vault encrypts sensitive values: create a file with ansible-vault create group_vars/webservers/vault.yml, edit it with ansible-vault edit, and run playbooks with ansible-playbook -i inventory.ini webserver.yml –ask-vault-pass. ansible-vault encrypt_string encrypts a single value inside an otherwise readable file. Since ansible-core 2.22, known secrets are also masked in Ansible’s output — a safety net, not a substitute for encryption.

Ansible vs Terraform — Which to Learn First?

Ansible and Terraform are often presented as rivals. They are not — most real environments use both. Terraform provisions infrastructure — server, network, load balancer. Ansible configures what runs on it — software, configuration files, services. If it exists in a cloud console, Terraform probably manages it; if it exists inside a server, Ansible probably does.

Our Terraform Tutorial for Beginners builds a VPC, a subnet and an EC2 instance. The natural next step is this tutorial: point Ansible at the instance Terraform created, and configure it. That two-tool workflow — Terraform to create, Ansible to configure — is the shape of most production infrastructure, and what employers expect. Which to learn first depends on your work: cloud infrastructure means Terraform first, existing servers mean Ansible first. For employability, learn both — our DevOps Engineer Roadmap 2026 puts them side by side as the two halves of infrastructure as code.

Common Beginner Mistakes

  • Running playbooks against production without –check –diff first — if it says “50 to change” and you expected two, stop.
  • Using shell or command for everything. Ansible has a purpose-built module for almost every task, and shell commands are not idempotent by default.
  • Secrets in plain text, or committed to Git. Use Ansible Vault from the first secret. Rotate anything ever committed unencrypted.
  • Forgetting become. Package installs and service management need privilege escalation — “Permission denied” usually means adding become: true.
  • Hard-coding hostnames and IPs. Put hosts in the inventory and values in variables — a playbook that only works on one server is a script, not automation.
  • Ignoring idempotency — a playbook reporting changed on every run has a problem, usually a shell task that should be a module.
  • Skipping the version check. Ansible moves twice a year — 2.22 needs Python 3.13 on the control node, and 2.19 stops receiving fixes on 30 November 2026.

FAQ

Do I need to install anything on the servers Ansible manages?

No. Ansible is agentless: it connects over SSH, copies the modules it needs to the host, runs them, and removes them. The managed node only needs SSH access and Python. Windows hosts are managed over WinRM instead of SSH.

Is Ansible free?

Yes, both are free and open source. Red Hat sells Ansible Automation Platform, a commercial product built on the same engine — but this tutorial uses only the free project.

What is the difference between ansible and ansible-core?

ansible-core is the minimal engine: the language, the runtime and the built-in modules. ansible is the community package that bundles ansible-core with a curated set of collections.

Can Ansible manage Windows servers?

Yes. Windows hosts are managed over WinRM and need PowerShell 5.1 to 7. The syntax is the same; only the modules change — ansible.windows.win_service instead of ansible.builtin.service.

How is a playbook different from a shell script?

A shell script lists steps; a playbook describes a desired end state. Ansible checks the current state before acting, skips work already done, runs tasks across many hosts in parallel, and reports what changed on each one.

Conclusion

You now have the complete Ansible loop: an inventory, ad-hoc commands that prove you can reach your servers, and a playbook that installs and starts a web server — idempotently, with variables, a template and a handler. From here: make it a role, encrypt secrets with Vault, put it in Git, and run it from a pipeline.

Next, combine the two halves of infrastructure as code: work through our Terraform Tutorial for Beginners and use Terraform to create the servers this playbook configures — the combination most DevOps job descriptions ask for.

Sources checked 27 Nov 2026: endoflife.date — ansible-core (2.22.0, 2 Nov 2026; 2.19 EOL 30 Nov 2026; Python 3.13–3.15 / 3.9–3.15); Ansible 15.0.0 release announcement, 17 Nov 2026; Ansible documentation at docs.ansible.com (Getting started with Ansible; Using Ansible playbooks). Re-check the latest ansible-core patch on the publication date.

—

Scroll to Top