keepcalculator

06 Numbers and Powers

Ansible for Beginners: A Simple Getting Started Guide

Card No. 06·56342 · Updated · Standard formula, shown below

To get started with Ansible, you install it on one control node, define your target servers in an inventory file, and run ad-hoc commands or playbooks that use SSH to configure those servers. This guide walks you through the core concepts, a first playbook, and common pitfalls so you can automate real tasks quickly and safely.

What Is Ansible and Why Use It?

Ansible is an open-source automation tool that configures servers, deploys software, and orchestrates workflows. Unlike many configuration management tools, it is agentless: you do not install any software on the machines you manage. Instead, the control node (the computer where Ansible runs) connects to target machines over SSH (or WinRM for Windows) and pushes small programs called modules that perform specific tasks. This makes Ansible easy to adopt because the only requirement on a managed host is a working SSH server and Python (on most systems).

It uses a declarative model: you describe the desired end state (e.g., “this package should be installed”) and Ansible figures out what changes are needed to get there. This is in contrast to imperative scripts where you write every step. The result is readable, version-controllable automation that can scale from one server to thousands.

How Ansible Works Under the Hood

Ansible’s architecture is simple but powerful. The main components are:

  • Control node – the machine where you run ansible or ansible-playbook. It can be your laptop, a VM, or a CI runner.
  • Inventory – a list of managed hosts, usually in INI or YAML format. It can be static (a file) or dynamic (pulled from cloud APIs).
  • Modules – small programs that Ansible executes on the target. Common ones include yum, apt, copy, service, and command.
  • Playbooks – YAML files that define a set of tasks to run on a group of hosts. They are the heart of Ansible.
  • Facts – system information (OS, IP, memory) that Ansible gathers automatically before running tasks, useful for conditional logic.

When you run a playbook, Ansible connects to each host, transfers the module’s code, executes it, and then removes the temporary files. This push-based model means no persistent daemon is needed, and it works over standard SSH, which is already ubiquitous in server environments.

Installing Ansible

Installation is straightforward on most Linux distributions and macOS. On a Debian/Ubuntu system, you can use:

sudo apt update
sudo apt install ansible

For Red Hat/CentOS/Fedora, use sudo dnf install ansible (or yum on older versions). On macOS, the easiest route is via Homebrew: brew install ansible. Windows is not officially supported as a control node, but you can use WSL (Windows Subsystem for Linux) or a small Linux VM. After installation, verify with ansible --version. You should see the version and the location of the config file.

One important note: Ansible is written in Python, so you need Python 3.8 or newer on the control node. Managed nodes need Python 2.7 or 3.5+ (though 2.7 is deprecated). Most modern Linux systems have Python preinstalled.

Setting Up Your First Inventory

The inventory file tells Ansible which hosts to manage. A simple static inventory can look like this (in INI format):

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

[databases]
db1.example.com

You can also define variables per host or group:

[webservers]
web1.example.com ansible_user=ubuntu
web2.example.com ansible_user=ubuntu

By default, Ansible uses your current username and SSH keys. If you need a password, you can use --ask-pass (but it is far better to use SSH keys). For testing, you can also define localhost in the inventory: localhost ansible_connection=local.

Your First Ad-Hoc Command and Playbook

Before writing a playbook, try an ad-hoc command to check connectivity:

ansible all -i inventory -m ping

This uses the ping module, which does not actually ping but verifies that Ansible can connect and run Python on the target. You should see a green SUCCESS for each host.

Now, let’s create a simple playbook that ensures Apache is installed and running on a Debian server. Save a file called webserver.yml:

---
- name: Ensure Apache is installed and running
  hosts: webservers
  become: yes
  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present
    - name: Start and enable Apache
      service:
        name: apache2
        state: started
        enabled: yes

Run it with:

ansible-playbook -i inventory webserver.yml

The become: yes tells Ansible to use sudo for those tasks. The playbook is idempotent: running it again will not reinstall Apache if it is already present, and it will only start the service if it is not running.

Common Problems and How to Avoid Them

Beginners often hit a few recurring issues:

  • SSH authentication failures – Make sure your SSH key is added to the target’s authorized_keys or use --ask-pass for testing. Also, the first connection may ask to confirm the host key; you can disable that with host_key_checking = False in ansible.cfg (not recommended for production).
  • Python interpreter issues – On some systems (especially older ones), Ansible may not find Python. You can specify the path in inventory: ansible_python_interpreter=/usr/bin/python3.
  • Privilege escalation – If you get permission denied, you likely need become: yes or become_user set correctly.
  • YAML syntax errors – Indentation is critical. Use spaces, not tabs, and check with ansible-playbook --syntax-check.

Another common misconception is that Ansible can only manage Linux. It can also manage Windows via WinRM, and network devices via dedicated modules, but the setup differs.

Comparing Ansible to Other Tools

Ansible is often compared to Puppet, Chef, and SaltStack. The key differences are:

  • Agentless – Ansible does not require a daemon on managed nodes, while Puppet and Chef typically do.
  • Push vs. pull – Ansible pushes configurations; Puppet and Chef usually have agents pull from a server.
  • Ease of learning – Ansible’s YAML syntax is considered simpler than Ruby-based Chef or Puppet’s DSL.
  • Scaling – Ansible can handle thousands of nodes, but very large fleets may benefit from pull-based models for efficiency.

For a small to medium environment, Ansible is often the fastest to get started with, and it doubles as a great tool for ad-hoc tasks.

Cost, Safety, and Best Practices

Ansible itself is free and open-source (Apache 2.0 license). Red Hat offers Ansible Automation Platform with paid support and extra features like a web UI and analytics, but the core engine is free. The main costs are time and infrastructure if you run a dedicated control node.

Safety is crucial when automating. Always test playbooks in a staging environment first. Use version control (e.g., Git) for your playbooks. Avoid hardcoding secrets; use Ansible Vault to encrypt sensitive variables. Be careful with become – only elevate privileges when necessary. Also, make sure your SSH keys are protected and rotated.

Another best practice is to use ansible-lint to catch common issues, and to structure your project with ansible-galaxy init for more complex roles.

Frequently Asked Questions

Do I need to install Ansible on every server?

No. Ansible is agentless. You install it only on the control node. Managed hosts just need SSH and Python (or WinRM for Windows). This is one of the biggest advantages over tools like Puppet or Chef.

Can I manage Windows servers with Ansible?

Yes, but you need to configure WinRM on the Windows hosts and use the win_* modules. The control node must be a Linux/macOS machine; Windows cannot be the control node natively. Also, you need to set ansible_connection=winrm in inventory.

What is the difference between ad-hoc commands and playbooks?

Ad-hoc commands are one-liners like ansible all -m command -a "uptime" – great for quick tasks. Playbooks are YAML files that define multiple tasks, can be reused, and support conditional logic, loops, and handlers. For anything beyond a one-off, use a playbook.

How do I handle secrets like passwords in playbooks?

Use Ansible Vault. You can encrypt a variable file or a whole playbook with ansible-vault encrypt. When running, you’ll be prompted for the vault password, or you can use a vault password file. Never put plaintext passwords in your inventory or playbooks.

Is Ansible good for large-scale environments?

Yes, Ansible can manage thousands of nodes, but you may need to tune performance (e.g., increase forks, use SSH multiplexing). For very large fleets, some teams use pull mode with ansible-pull to reduce load on the control node.

What is a role in Ansible?

A role is a structured way to organize playbooks, variables, templates, and handlers into reusable components. For example, a webserver role might contain tasks to install Apache, templates for virtual hosts, and handlers to restart the service. Roles make your automation modular and shareable.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →