AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Getting Started with Ansible: First Playbook in 10 Minutes

By Luca Berton · Published 2024-01-01 · Category: installation

Get started with Ansible in 10 minutes. Install Ansible, write your first inventory and playbook, understand modules, and automate your first server.

Getting Started with Ansible: First Playbook in 10 Minutes

What is Ansible?

If you're new to automation and configuration management, Ansible is one of the best tools to start with. It’s agentless, uses YAML, and it's designed to be simple, powerful, and efficient. Whether you’re managing a few servers or deploying applications to hundreds, Ansible has got you covered.

I'm Luca Berton, and welcome to today’s episode of Ansible Pilot.

See also: Ansible 'Missing Required Arguments' Error: Fix Missing Module Parameters

Why use Ansible?

Here are some key reasons Ansible is loved by system administrators and DevOps engineers:

No agents required – everything works over SSH or WinRM. Simple YAML syntax – great for beginners. Powerful modules – support for system packages, services, files, cloud, and more. Idempotency – run your playbook multiple times without side effects. Extensibility – build roles, collections, and plugins as your infrastructure grows.

Key Components of Ansible

Let’s break down Ansible into its most important building blocks:

Inventory: A file (usually hosts) listing the machines you want to manage. Modules: Pre-built units of work – think of them like commands (ping, copy, yum, etc.). Playbooks: YAML files where you define your automation tasks. Tasks: Individual units of work within a playbook. Roles: Reusable collections of tasks, files, vars, and more. Collections: Namespaced bundles of roles, modules, and plugins.

See also: Ansible Documentation: Complete Reference & Quick Start Guide (2026)

Prerequisites

A control node (Linux/macOS with Python 3.x). Managed nodes (Linux or Windows). SSH access to Linux targets, or WinRM setup for Windows.

Your First Ansible Playbook

Let’s walk through a very basic playbook to install Apache on a group of web servers.

install_apache.yml

---
- name: Install Apache Web Server
  hosts: webservers
  become: true
  tasks:
    - name: Install Apache
      ansible.builtin.yum:
        name: httpd
        state: present

- name: Start Apache ansible.builtin.service: name: httpd state: started enabled: true

Run the Playbook

ansible-playbook -i inventory.ini install_apache.yml

Where inventory.ini might look like:

[webservers]
192.168.1.10
192.168.1.11

See also: Ansible 'chgrp failed' Error: Fix Group Ownership Permission Issues

Best Practices

Use roles and collections to organize large projects. Keep inventories dynamic when working with cloud providers. Secure secrets using Ansible Vault. • Use gather_facts: false if facts aren't needed – it speeds up execution.

Conclusion

That’s your first step into the world of Ansible! You’ve learned what Ansible is, how it works, and how to write your first playbook. Start simple, iterate fast, and scale confidently.

Step 1: Install Ansible

# Ubuntu/Debian
sudo apt update && sudo apt install -y ansible

# RHEL/CentOS/Fedora sudo dnf install -y ansible

# macOS brew install ansible

# pip (any platform) pip install ansible

Verify: ansible --version

Step 2: Create Your Inventory

# inventory.yml
all:
  children:
    webservers:
      hosts:
        web1:
          ansible_host: 192.168.1.10
        web2:
          ansible_host: 192.168.1.11
    dbservers:
      hosts:
        db1:
          ansible_host: 192.168.1.20
  vars:
    ansible_user: deploy

Step 3: Test Connectivity

ansible all -i inventory.yml -m ping

Step 4: Run Ad-Hoc Commands

ansible all -i inventory.yml -m command -a "uptime"
ansible webservers -i inventory.yml -m apt -a "name=nginx state=present" --become

Step 5: Write Your First Playbook

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

- name: Start nginx ansible.builtin.service: name: nginx state: started enabled: true

- name: Deploy index page ansible.builtin.copy: content: "<h1>Hello from {{ inventory_hostname }}</h1>" dest: /var/www/html/index.html

Run it: ansible-playbook -i inventory.yml site.yml

Key Concepts

| Concept | Description | |---------|-------------| | Inventory | List of managed hosts and groups | | Playbook | YAML file defining automation tasks | | Task | Single action (install, copy, restart) | | Module | Pre-built tool (apt, copy, service) | | Role | Reusable package of tasks and files | | Handler | Task triggered by change notifications | | Facts | System info gathered from hosts | | Become | Privilege escalation (sudo) |

Project Structure

ansible-project/
  ansible.cfg
  inventory.yml
  site.yml
  group_vars/
    all.yml
    webservers.yml
  roles/
    webserver/
      tasks/main.yml
      handlers/main.yml
      templates/

FAQ

Do I need to install anything on managed hosts?

No - Ansible is agentless. It only needs SSH and Python on remote hosts.

What's the difference between a playbook and a role?

A playbook is a complete automation file. A role is a reusable, shareable package. Playbooks include roles.

How do I handle passwords securely?

Use Ansible Vault:

ansible-vault create secrets.yml
ansible-playbook site.yml --ask-vault-pass

Prerequisites

Control node: Linux/macOS with Python 3.8+ • Managed nodes: SSH access + Python 3 installed • No agent required on managed hosts

Quick Start (5 Minutes)

# 1. Install
pip install ansible
# or: sudo apt install ansible

# 2. Test connection ansible localhost -m ping

# 3. Run ad-hoc command ansible localhost -m debug -a "msg='Hello Ansible!'"

Configure Inventory

# inventory.yml
all:
  children:
    webservers:
      hosts:
        web1:
          ansible_host: 192.168.1.10
        web2:
          ansible_host: 192.168.1.11
      vars:
        ansible_user: deploy
# Test connectivity
ansible -i inventory.yml webservers -m ping

First Playbook

# playbook.yml
---
- name: Configure web servers
  hosts: webservers
  become: true

tasks: - name: Update apt cache ansible.builtin.apt: update_cache: true cache_valid_time: 3600

- name: Install packages ansible.builtin.apt: name: - nginx - curl - htop state: present

- name: Start nginx ansible.builtin.service: name: nginx state: started enabled: true

- name: Deploy welcome page ansible.builtin.copy: content: | <h1>Welcome to {{ inventory_hostname }}</h1> <p>Managed by Ansible</p> dest: /var/www/html/index.html

ansible-playbook -i inventory.yml playbook.yml

Ad-Hoc Commands

# Check disk space on all servers
ansible webservers -i inventory.yml -a "df -h"

# Install a package ansible webservers -i inventory.yml -m apt -a "name=vim state=present" --become

# Copy a file ansible webservers -i inventory.yml -m copy -a "src=config.yml dest=/tmp/"

# Restart a service ansible webservers -i inventory.yml -m service -a "name=nginx state=restarted" --become

# Gather facts ansible web1 -i inventory.yml -m setup

ansible.cfg

# ansible.cfg (in project directory)
[defaults]
inventory = inventory.yml
remote_user = deploy
host_key_checking = false

[privilege_escalation] become = true become_method = sudo

Project Layout

my-project/
├── ansible.cfg
├── inventory.yml
├── playbook.yml
├── group_vars/
│   └── webservers.yml
├── host_vars/
│   └── web1.yml
├── roles/
│   └── nginx/
│       ├── tasks/main.yml
│       ├── handlers/main.yml
│       ├── templates/nginx.conf.j2
│       └── defaults/main.yml
└── files/
    └── index.html

Essential Modules

| Module | Purpose | Example | |--------|---------|---------| | apt/yum | Package management | Install software | | copy | Copy files | Deploy configs | | template | Jinja2 templates | Dynamic configs | | service | Manage services | Start/stop/restart | | file | File/directory ops | Create, chmod, chown | | command | Run commands | Execute scripts | | user | User accounts | Create/manage users | | git | Git operations | Clone repositories |

Next Steps

Variables: Use group_vars/ and host_vars/ for per-environment config Roles: Organize playbooks into reusable roles Ansible Galaxy: Install community roles with ansible-galaxy Ansible Vault: Encrypt sensitive data (passwords, keys) AWX/AAP: Add a web UI for team collaboration

FAQ

Do I need root access?

Use become: true for tasks that need sudo. The SSH user needs sudo privileges on managed hosts.

Can I test without real servers?

Yes — use ansible localhost or Docker containers. Molecule is great for testing roles.

How is Ansible different from shell scripts?

Ansible is idempotent — running the same playbook twice produces the same result. It also handles connection management, error handling, and reporting.

Step 1: Install

# pip (recommended)
pip install ansible

# Ubuntu/Debian sudo apt install ansible

# macOS brew install ansible

# Verify ansible --version

Step 2: Create Inventory

# inventory.ini
[webservers]
web1 ansible_host=192.168.1.10 ansible_user=deploy

[all:vars] ansible_python_interpreter=/usr/bin/python3

Step 3: Test Connection

ansible all -i inventory.ini -m ping
# web1 | SUCCESS => { "ping": "pong" }

Step 4: First Playbook

# first-playbook.yml
---
- name: My first playbook
  hosts: webservers
  become: true
  tasks:
    - name: Ensure nginx is installed
      apt:
        name: nginx
        state: present
        update_cache: true

- name: Ensure nginx is running service: name: nginx state: started enabled: true

- name: Deploy a web page copy: content: "<h1>Automated by Ansible!</h1>" dest: /var/www/html/index.html

ansible-playbook -i inventory.ini first-playbook.yml

Step 5: Use Variables

- hosts: webservers
  become: true
  vars:
    app_name: mywebapp
    app_port: 8080
  tasks:
    - debug:
        msg: "Deploying {{ app_name }} on port {{ app_port }}"

Key Concepts

# Ad-hoc command (one-off)
ansible all -i inventory.ini -m command -a "df -h"

# Playbook (repeatable) ansible-playbook -i inventory.ini deploy.yml

# Check mode (dry run) ansible-playbook -i inventory.ini deploy.yml --check

# Verbose output ansible-playbook -i inventory.ini deploy.yml -vv

Next Steps

Learn modulesapt, yum, copy, template, service, file, user Use roles — Organize playbooks into reusable components Explore Galaxy — Install community roles: ansible-galaxy install geerlingguy.docker Encrypt secrets — Use Ansible Vault: ansible-vault encrypt secrets.yml Set up ansible.cfg — Customize defaults for your project

FAQ

Do I need an agent on remote hosts?

No — Ansible is agentless. It connects via SSH and requires only Python on the remote host.

Can I test locally?

Yes — use hosts: localhost and connection: local to run playbooks on your own machine.

What's the difference between ansible and ansible-playbook?

ansible runs ad-hoc commands (single tasks). ansible-playbook runs YAML playbook files (multi-task workflows).

Related Articles

patching Windows with AnsibleAnsible Vault CLI referenceswitching users with Ansible becomeorganizing hosts with Ansible inventoryrole dependencies in Ansible

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home