arrow_back
Back

Ansible: playbooks, inventory, roles, and automation

Andrew Dorokhov Andrew Dorokhov schedule 2 min read
menu_book Table of Contents

open_in_new Ansible is an open-source automation tool for configuring servers, deploying applications, and orchestrating tasks across many hosts. It uses SSH by default and does not require a permanent agent on managed machines — only Python and SSH access.

Core ideas

  • Inventory — a list of hosts or groups (often inventory.ini or YAML).
  • Playbooks — YAML files that describe what should happen on which hosts.
  • Tasks — individual steps inside a play (install a package, copy a file, restart a service).
  • Modules — small units of work Ansible ships with (e.g. apt, copy, service). Tasks call modules with parameters.

Runs are designed to be idempotent: running the same playbook twice should leave the system in the desired state without duplicating side effects.

Minimal inventory

[web]
web1.example.com
web2.example.com

[web:vars]
ansible_user=deploy

Minimal playbook

---
- name: Ensure nginx is installed
  hosts: web
  become: true
  tasks:
    - name: Install nginx
      ansible.builtin.apt:
        name: nginx
        state: present
        update_cache: true

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

Run it with:

ansible-playbook -i inventory.ini site.yml

When to use it

Ansible fits well for infrastructure as code on bare metal or VMs, repeatable deployments, and glue between CI and servers. For container-centric workflows it is often combined with image builds and orchestrators (Kubernetes, Docker Swarm), while Ansible handles host bootstrap and configuration outside the cluster.

Running Ansible via Docker

You can run Ansible without installing it locally by using a Docker image. This can be useful for CI/CD pipelines or keeping your environment clean.

Here’s how to run a playbook using Docker:

docker run --rm -it \
  -v ~/.ssh:/root/.ssh \
  -v ~/.aws:/root/.aws \
  -v "$PWD:/apps" \
  -w /apps \
  alpine/ansible:latest \
  ansible --help

This command mounts your current directory into the container, so the playbook and inventory files are available.

See also

Official docs: open_in_new docs.ansible.com .

code

Need Help with Development?

Happy to help — reach out via the contacts or go straight to my Upwork profile.

work View Upwork Profile arrow_forward
Next Article

Terraform: infrastructure as code, state, and providers

arrow_forward