Aug 23, 2018

38 min read

A Step By Step Guide To Ansible (Tutorial)

Written by

Vippy The VPS

In today's Ansible for beginners tutorial, we're going to guide you through everything you need to know in one sitting – from installing Ansible on your machine all the way to setting up and running an example Playbook.

Then we'll talk a bit about how to best put it to use on your VPS or multiple VPSs.

Getting started with Ansible might seem a bit daunting, but once you master its many options and complexities, you'll have a whole new level of control when configuring servers.

So – without further ado, let's jump right into our comprehensive, step-by-step Ansible guide...

Notes On The Ansible Tutorial

  • This tutorial requires the use of domain names. Whenever you see the SUBDOMAIN, DOMAIN, or TLD variables, replace them with the details of your domain name. In, the SUBDOMAIN is example, ssdnodes is the DOMAIN and .com is the TLD.
  • This tutorial requires the use of IP address. Whenever you see the IP_ADDRESS variable, replace it with your own.

What Is "Infrastructure As Code"?

Infrastructure as code (IaC) is the way of defining computing and network infrastructure through source code, the same way you do for applications. Rather than manually configuring your infrastructure or using a one-off isolated script, IaC gives you the power to write code, using a high-level language, to decide how infrastructure should be configured and deployed.
IaC is different from infrastructure automation, which involves repeating the steps multiple times and spawning them on several servers.

The guiding principle behind IaC is to enforce consistency among DevOps team members by representing the desired state of their infrastructure via code. Moreover, the code can be kept in source control, which means it can be audited, tested on, and used to create reproducible builds with continuous delivery.

What Is Ansible?

Ansible is an open source IT configuration management, deployment, and orchestration tool. It empowers DevOps teams to define their infrastructure as a code in a simple and declarative manner.

A lot of people compare Ansible to similar tools like Chef or Puppet. They all help automate and provision infrastructure, but there are a few features that make me prefer Ansible over the others.

What Are The Benefits Of Using Ansible?

Ansible Is Agentless

Ansible doesn’t need any agents to be installed on remote systems to be managed, which means less maintenance overhead and performance issues. Instead, Ansible uses a push-based approach leveraging existing SSH connections to run tasks on the remote managed host. Chef or Puppet work by installing an agent on the hosts to be managed and the agent pulls changes from the control host using its own channel.

Ansible Is Written In Python

Ansible is written in Python, which means installing and running Ansible in any Linux distribution is very easy, and only a little more difficult on OS X. Being a popular language, there’s also a good chance that you’re familiar with it, or at least can find enough resources online to start learning. Or, you’ll easily be able to find a developer with Python experience to help you out.

Ansible Is Easy To Learn

The fact that a new user can get up to speed and run Ansible tasks in a matter of minutes, thanks to clear and easy-to-follow documentation, is one of the most appealing features of Ansible. Troubleshooting in Ansible is also very easy for beginners, and the fact that all tasks are idempotent reduces the risk of making a mistake.

Deploy Infrastructure In Record Time

Ansible can dispatch tasks to multiple remote managed hosts in parallel. This means you can execute Ansible tasks on a second managed host without waiting for them to complete on the first to reduce provision time and deploy your infrastructure faster than ever.


Step 1: Install Ansible On Your Control Machine

To take your first steps with Ansible, you first need to install it on your control machine. This is the machine you’ll use to dispatch tasks. For most people, this will be your desktop machine at home or your laptop, but you can also use one VPS as a control host to connect to other VPSs.

Installing Ansible on Ubuntu 16.04

You can install Ansible using standard package managers like apt/yum or Python’s pip command. To install it using standard package manager in Ubuntu, add its repository information apt-add-repository. Next, update the system and install Ansible using apt-get.

$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

Installing Ansible on CentOS 7

While installing Ansible in CentOS and RHEL, you need to enable the EPEL repository first before proceeding with installation of Ansible. Once you enabled the EPEL repository, install Ansible using yum.

$ cd /tmp
$ wget
$ sudo rpm -ivh epel-release-7-9.noarch.rpm
$ sudo yum update
$ sudo yum install ansible

Guide to working with Ansible

You are now up and running with Ansible in your system, which is otherwise known as the control host. The control host is the Ansible host you to dispatch tasks to the remote managed Ansible hosts.

Before you start delegating tasks to a managed host, make sure you have non-root, a sudo-enabled user on that host—it’s always a bad idea to connect to a remote VPS via a root user.

Ansible inventory files

The Ansible inventory file lists which hosts will receive commands from the control host. The inventory can list individual hosts, or group them under categories you distinguish.

The default location for the inventory file is /etc/ansible/hosts, but it’s also possible to change the location of the inventory file by uncommenting the inventory parameter in /etc/ansible/ansible.cfg

A typical inventory file can list the managed host either by IP address or by domain names. It is also possible to list one managed host in more than one group. Here’s an example of listing two hosts under the webservers and dbservers categories.



To test if all the hosts are discoverable by the inventory file, use the following ad-hoc command.

$ ansible all --list-hosts
  hosts (2):

You can also list the hosts by group name:

$ ansible dbservers --list-hosts
hosts (2):

Ad-hoc commands in Ansible are merely those that perform a single command across one or many hosts. They don’t use tasks but allow you to do a lot of things quite easily without building out playbooks (more on those in the second part of this guide).

To find out if all the hosts are up and running, use the following ad-hoc command that uses the ping module

Continue reading this article
by subscribing to our newsletter.
Subscribe now

A note about tutorials: We encourage our users to try out tutorials, but they aren't fully supported by our team—we can't always provide support when things go wrong. Be sure to check which OS and version it was tested with before you proceed.

If you want a fully managed experience, with dedicated support for any application you might want to run, contact us for more information.