No history yet

Introduction to Ansible

What Is Ansible?

Imagine you're in charge of a single computer server. Keeping it updated and configured correctly is pretty straightforward. Now, what if you had to manage ten servers? Or a hundred? Suddenly, that simple task becomes a massive headache. Logging into each machine to perform the same set of commands is slow, repetitive, and prone to human error.

This is the problem Ansible solves. It's an automation tool that helps you manage fleets of computers as easily as you manage one. You tell Ansible what you want your systems to look like, and it handles the rest. This includes tasks like installing software, updating configurations, deploying applications, and orchestrating more complex workflows.

Ansible is a software platform that automates many of the manual and repetitive IT tasks in your organization.

How Ansible Works

Ansible’s biggest advantage is its simplicity. Many other automation tools require you to install special software, called an “agent,” on every machine you want to manage. Ansible is agentless. It communicates with your servers over standard, secure channels that are almost always already available: SSH for Linux and macOS, and WinRM for Windows.

This means you don't have to worry about installing, updating, or securing yet another piece of software on your managed machines. The setup is cleaner and more secure from the start.

The machine where you install and run Ansible is called the control node. The servers it manages are called managed nodes. From a single control node, you can manage hundreds or even thousands of machines.

Ansible is also idempotent. This is a fancy way of saying that if you run the same operation multiple times, you’ll always get the same result. If a package is already installed or a file is already configured correctly, Ansible won't make any changes. This makes automation predictable and safe. You can run your instructions again and again without worrying about breaking something.

Playbooks and YAML

You don't write traditional code in Ansible. Instead, you write instructions in files called playbooks. These playbooks are written in YAML (YAML Ain't Markup Language), which is a human-readable data format. It uses simple indentation (spaces, not tabs) to define structure, making it easy to see what's going on at a glance.

A playbook is a list of tasks that you want to execute on your managed nodes. Each task calls an Ansible module, which is a reusable unit of code that performs a specific action, like installing a package or starting a service. There are thousands of modules available for managing everything from users and files to cloud infrastructure and network devices.

# This is a simple playbook in YAML
- name: Configure web server
  hosts: webservers
  become: true
  tasks:
    - name: Ensure apache is at the latest version
      ansible.builtin.yum:
        name: httpd
        state: latest

    - name: Start and enable the apache service
      ansible.builtin.service:
        name: httpd
        state: started
        enabled: yes

Even without knowing Ansible, you can probably guess what that playbook does. It targets a group of servers called webservers, ensures the httpd (Apache) package is installed, and then makes sure the service is running.

Setting Up Your Environment

Getting started with Ansible requires setting up a control node. This can be your laptop, a virtual machine, or any server with a Linux-based operating system or macOS. Ansible cannot be run on a Windows control node.

Installation is typically straightforward. On most Linux systems, you can install it using the system's package manager. For example, on Ubuntu or Debian:

sudo apt update
sudo apt install ansible -y

Once Ansible is installed, the next step is to create an inventory file. This file is a list of the managed nodes that Ansible will connect to. It's usually located at /etc/ansible/hosts, but you can create one in your project directory. The simplest inventory is a list of IP addresses or hostnames.

# An example inventory file named 'hosts'

[webservers]
192.168.1.10
192.168.1.11

[database]
db.example.com

This inventory defines two groups: webservers and database. Grouping servers lets you run tasks on specific sets of machines. Before Ansible can connect, you must ensure your control node can reach these managed nodes over SSH, typically by setting up SSH keys for passwordless authentication.

With Ansible installed and a basic inventory file configured, you're ready to start automating.

Quiz Questions 1/5

What is the primary problem Ansible is designed to solve?

Quiz Questions 2/5

In Ansible terminology, what is a "control node"?