How to Install and Configure Ansible Server and Hosts

install and configure ansible

This guide will teach you to install and configure Ansible control server on a Ubuntu/Centos/Redhat/fedora  servers. Also, we will see how to configure hosts that have to be managed by Ansible server. I would strongly suggest using Vagrant for all Ansible test purposes.

Install and Configure Ansible

Choose any of the following three methods for installing Ansible based on your operating system.

1. Using Pip

If you have python pip in your system, use the following pip command.

sudo pip install ansible

2. Ubuntu

Execute the following commands to install ansible.

sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible

3. Redhat/Centos/Fedora

Redhat/Centos 6

sudo yum -y install ansible

Redhat/Centos 7

sudo yum -y install epel-release
sudo yum -y  install ansible

Configuring hosts

Here I represent all the servers that have to be managed using Ansible as hosts. Ansible keeps track of the hosts using the inventory file ( A file with list of servers). It has the IP address/domain name of the hosts with username, password or key information to connect to the node.

All the default configurations are present in /etc/ansible/ansible.cfg file. Here you can change all the default paths if you want your own custom paths and configurations.

Now let’s disable host key checking by replacing the host_key_checking parameter to true so that Ansible won’t prompt for host key checking. This is not recommended for production deployments

sed -i '/#host_key_checking = False/c\host_key_checking = True' /etc/ansible/ansible.cfg

By default the inventory file named hosts is present in /etc/ansible/ directory. If you open the /etc/ansible/hosts you will find the sample host entries. Lets keeps this file as a backup and create our own hosts file.

READ  Puppet Hiera Tutorial - Beginners Guide

Let’s create the backup of original hosts inventory file.

sudo mv /etc/ansible/hosts /etc/ansible/hosts.original

Create a new hosts file.

sudo touch /etc/ansible/hosts

Now we have an empty hosts inventory file. Let’s create it from scratch.

In every environment the servers are segregated as web group, DB group, app group etc.. we can have similar segregation in our inventory file using labels. It is a recommended way of managing servers. For instance, you might have dev, test and prod servers. In this case, you can group servers from different environments under different labels.

Create two new hosts, get its IP address, username, and password/key. We will use these new hosts for testing.

Now we have the Ansible control server and two hosts that need to be managed. My hosts IP addresses are 192.168.2.30 and 192.168.2.40 and I will be using it throughout the example. Replace it with your hosts IP address.

Define the hosts in /etc/ansible/hosts inventory file with dev, dev:vars and local label as shown below.

[dev]
192.168.2.30
192.168.2.40

[dev:vars]
ansible_user=vagrant
ansible_ssh_pass=vargrant

[local]
127.0.0.1

dev:vars parameters are applied to the servers under dev label. As we know that Ansible uses ssh for connecting to hosts. So we need to specify the username and password of those hosts. If all the servers have the same username and password, you can mention it in dev:vars label. If not you can specify it with the IP addresses separated by space as shown below.

192.168.2.30 ansible_user=vagrant ansible_ssh_pass=vargrant

the local label represents the Ansible server itself. So if you want to run a playbook on your ansible server, you can make use of the local label.

READ  Chef Cookbook Testing Tutorial - Getting Started Guide

Test the Configuration Using an Ad-Hoc Command

Now we have every Configuration in place. Let’s test our configuration using the following command.

ansible all -m ping

You should get the following success message.

[email protected]:/etc/ansible$ ansible all -m ping
192.168.2.30 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
192.168.2.40 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.

Leave a Reply

Your email address will not be published. Required fields are marked *