A Step By Step Guide To Ansible (Tutorial)
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 example.ssdnodes.com, 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"?
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?
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 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
$ 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
$ cd /tmp $ wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm $ 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
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] 184.108.40.206 SUBDOMAIN.DOMAIN.TLD
To test if all the hosts are discoverable by the inventory file, use the following ad-hoc command.
$ ansible all --list-hosts hosts (2): 220.127.116.11 SUBDOMAIN.DOMAIN.TLD
You can also list the hosts by group name:
$ ansible dbservers --list-hosts hosts (2): 18.104.22.168 SUBDOMAIN.DOMAIN.TLD
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 of Ansible. The
-u switch specifies which user Ansible will connect to via
by subscribing to our newsletter.
Share this post:Reddit Hacker News Facebook Twitter Flipboard
Like what you saw? Subscribe to our weekly newsletter.