Ansible is a powerful yet straightforward automation tool that can save you countless hours of repetitive system administration tasks.
In this guide, you'll learn the essentials of Ansible and complete a practical project by setting up a web server automatically.
By the end of this article, you'll have hands-on experience with Ansible's core concepts and a working web server deployment you can build upon for your own projects.
What is Ansible?
Ansible is an open-source automation platform that simplifies configuration management, application deployment, and task automation.
Unlike some other automation tools, Ansible doesn't require special software to be installed on the machines you want to manage. Instead, it communicates with your systems using SSH, making it significantly easier to get started.
At its core, Ansible works by sending instructions from a control machine (your computer) to target machines (servers you want to manage). These instructions, written in YAML, tell the target machines exactly what state they should be in—what software should be installed, how services should be configured, and so on.
Why use Ansible?
The beauty of Ansible lies in its simplicity. Ansible is agentless, meaning you don't need to install and maintain client software on your servers. It uses YAML syntax, which is human-readable and easy to learn even for beginners.
Ansible operations are idempotent, so you can run the same instructions multiple times without causing problems—it only makes changes when needed.
Additionally, Ansible comes with hundreds of built-in modules for managing various systems and services, making it versatile for different automation needs.
Setting up your environment
Before you can start automating, you'll need to install Ansible on your control machine (the computer from which you'll run your commands).
I'll focus on installation for Linux and macOS, as these are the most common platforms for Ansible controllers.
For Ubuntu/Debian systems:
For macOS (using Homebrew):
Verifying your installation
Once installed, verify that Ansible is working correctly by checking its version:
You should see output similar to this:
Understanding the inventory
The inventory is simply a list of systems that Ansible will manage. While Ansible supports dynamic inventories that can pull server information from cloud providers or other sources, we'll start with a basic static inventory file.
Creating a simple inventory file
Create a new file named inventory.ini in your working directory:
This inventory defines a group called webservers containing one server with IP
192.168.1.10, a group called database with one server, and variables that
apply to all servers, specifying how to connect via SSH.
For beginners, it's perfectly fine to start with just one server. You can even use localhost for testing:
Testing connectivity
Let's verify that Ansible can communicate with your servers using the ping
module:
If successful, you should see:
If you see "pong" in the response, your connection is working correctly. If you encounter errors, check your SSH configuration and make sure you can manually SSH into the target servers.
Running ad-hoc commands
Before diving into playbooks, let's explore ad-hoc commands. These are one-liner tasks that you might need to perform quickly across multiple servers.
Basic command structure
Ad-hoc commands follow this structure:
Let's try some useful ad-hoc commands, starting with checking system information:
This command returns detailed information about your target servers, including operating system, memory, and network configuration.
Another command ad-hoc command is one that installs a package. On Ubuntu/Debian, use:
This installs Nginx on your webservers. The --become flag tells Ansible to use
privilege escalation (sudo).
Finally, you can create a file on your target servers with:
This creates a file with the specified content in the /tmp directory.
Ad-hoc commands are great for quick tasks, but for repeatable operations, playbooks are the way to go so lets explore that next.
Writing your first playbook
Playbooks are Ansible's configuration, deployment, and orchestration language. They describe a policy you want your systems to enforce, using a simple, declarative format.
To get started, create a file named webserver.yml:
This simple playbook targets the webservers group from your inventory,
requests privilege escalation (sudo), updates the apt cache if it hasn't been
updated in the last hour, installs Nginx, and makes sure Nginx is running and
set to start on boot.
Once saved, execute your playbook with:
You should see output showing the execution of each task:
Congratulations! You've just automated the installation and configuration of a web server. If you run the playbook again, you'll notice most tasks report as "ok" rather than "changed"—this is Ansible's idempotence at work. It only makes changes when needed.
Expanding your web server project
Now let's enhance our web server by adding a custom homepage and configuring firewall rules.
First, let's create a templates directory and a template for our custom homepage:
Create templates/index.html.j2:
Now, update your playbook to use this template:
The template module uses Jinja2 templating to replace variables in your template
with actual values. The variables with ansible_ prefix are gathered by Ansible
automatically as facts about the system.
Also notice the introduction of a handler. Handlers are special tasks that only run when notified by another task, typically used for restarting services when their configuration changes.
Configuring firewall rules
Let's add firewall configuration to our playbook to allow HTTP traffic:
This updated playbook installs both Nginx and UFW (Uncomplicated Firewall), creates a custom index page, configures the firewall to allow HTTP and HTTPS traffic, enables the firewall with a default deny policy, and ensures Nginx is running.
Testing your completed web server
Run your updated playbook:
After the playbook completes, open a web browser and navigate to your server's IP address. You should see your custom homepage with the server's information.
If everything works correctly, you've successfully used Ansible to install necessary packages, configure a web server, deploy custom content, and set up security rules.
Troubleshooting common issues
Even with a tool as straightforward as Ansible, you might encounter some issues. Here are some common problems and how to solve them:
Connection problems
If Ansible can't connect to your servers, check SSH connectivity by making sure you can manually SSH into the target servers. Verify hostnames and IP addresses in your inventory file, and ensure your SSH key or password authentication is working.
For more detailed connection debugging, add the -vvv flag:
Permission errors
If tasks fail due to permission issues, check that the become: yes directive
is included in your playbook. Ensure your user has sudo privileges on the target
system. For specific files, check ownership and permissions.
Syntax mistakes
YAML is sensitive to indentation. If you get syntax errors, use a YAML linter or editor with YAML support. Check your indentation (spaces, not tabs) and verify that all colons, dashes, and quotes are correctly placed.
You can validate your playbook syntax without running it:
Final thoughts
Ansible makes automation accessible even to beginners. Through this guide, you've learned how to install and configure Ansible, create an inventory of managed systems, run one-off commands across multiple servers, write playbooks for repeatable configurations, and deploy a working web server with custom content and security settings.
The skills you've gained provide a foundation for more complex automation tasks. To further develop your Ansible skills, explore Ansible's official documentation and experiment with different modules from the module library.
Think about repetitive tasks in your workflow that could be automated. You might want to deploy your favorite application stack, set up regular backups, configure monitoring tools, or standardize security settings across servers.
By investing time in learning Ansible, you're taking a significant step toward more efficient and consistent system administration. Happy automating!