How to Install and Configure Ansible

How to Install and Configure Ansible on Ubuntu 16.04.

Configuration Management is a process of maintaining the state of the system. For example, The Configuration management system will ensure the system or server’s consistency by maintaining the configuration and setup of the machine or change on the same as per the requirement. Ansible is the configuration management tool which gives automation and systematic approaches to the configuration management process. As compared to other configuration Management tools, Ansible is simple and easy to use. In this article, we will see how to install and configure on ubuntu machine.

Architecture of Ansible

Ansible has four main components called Modules, Inventory, Plugin, and API. Modules are the setoff commands that are installing package or configuration into the system. Inventory is a file which contains the information of hosts and set of hosts where tasks, commands, and modules are configured. Plugins are the running with ansible code functionality and help ansible to run ansible tasks as a step. API is the medium of communication for the private and public cloud instances.

Ansible Architecture

User can run the Ansible by running Ansible playbook written in YAML file which will look for the inventory file for the target hosts. Ansible uses SSH to make the connection with target machines and workstation. Hence Ansible system agentless and it is called as a push based system.

Requirments

Since Ansible is an Agentless Push based system, we only need to configure the workstation and the target host’s hostname or network address is enough for the setup.

Install and Configure Ansible on Ubuntu

Step 1: PPA addition.

As the first step of ansible installation, we need to add ansible’s PPA repository to the APT. Run the following command to add the PPA repository.

sudo apt-add-repository ppa:ansible/ansible

Step 2: Update APT and install ansible

Once after adding the Ansibles PPA, Update the APT’s index. By running following command.

sudo apt-get update

Then Install the Ansible by passing following command.

sudo apt-get install ansible

Step 3: Configuring Ansible Hosts

Ansible will look for all the target machines from the ‘hosts‘ file. For the same, we need to set up the ‘hosts’ file first. Open the ‘hosts‘ file by passing the following command.

sudo nano /etc/ansible/hosts

Add the following lines at the end of the hosts file

[nodes]
node1 ansible_ssh_host=192.168.10.11
note2 ansible_ssh_host=192.168.10.12
note3 ansible_ssh_host=192.168.10.13

The above lines are adding three nodes (node1, node2, node3) and the respective ssh IP address (192.168.10.11, 192.168.10.12, 192.168.10.13) in the ‘hosts‘ list.

Step 4: Configuring User

By Default, when you run ansible, it will use the current User of the host machine. But Ansible needs root user for the connection. We need to tell ansible to use the root user for the connections. For the same, create a folder “group_vars” and create a file named as “nodes”.

sudo mkdir /etc/ansible/group_vars
sudo nano /etc/ansible/group_vars/nodes

Then add the following likes in the servers file.

---
ansible_ssh_user: root

This line is in YAML format so that it should start with “---

Step 5: Test Ansible Installation

Let us test the Ansible installation. We will use the simple module ping to test the ansible here. Pass the following command to ping all the nodes mentioned in the “hosts” file.

ansible -m ping all

You will get the following result.

node1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

node2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

node3 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

We can also mention the specific group of nodes in the command to run the ansible module on the nodes. For the same create the group in the “hosts” file,

[group1]
node1 ansible_ssh_host=192.168.10.11
note3 ansible_ssh_host=192.168.10.13

define the root user access in the “ansible_var” folder as mentioned below.

sudo nano /etc/ansible/group_vars/group1

and add the following lines

---
ansible_ssh_user: root

Then we can call the individual host by mentioning the name of the host group like below.

ansible -m ping group1

Conclusion

We have covered how to Install and Configure Ansible on Ubuntu and how to configure the ansible in terms of creating “hosts"file and “ansible_var” for user configuration. We will discuss more on how to write playbook, modules, roles, templates and other important features in upcoming ansible articles.

Stay tuned and subscribe DigitalVarys for the latest update on DevOps, DevSecOps and Agile topics.

Leave a Reply