Ansible

How to Install Ansible on Ubuntu 18.04

What is Ansible?

Ansible is an open-source tool for server provisioning, configuration management, and automatic application deployment.

It’s a simple tool that makes your life as system administration easier, improves the scalability, consistency, and reliability of your IT system environment.

Ansible is written in Python, can run on Unix-like operating systems such as Ubuntu, CentOS, and RHEL.

Ansible is an agentless configuration management tool. It runs through SSH service and can manage and provision both Unix-like systems and Microsoft Windows.

Why using Ansible?

Nowadays, automatic deployment is everything in IT industries. And ansible comes helps you to manage your complex IT infrastructures. It is powerful and easy to set up and use.

Ansible was flexible because it can deploy your application on the complex environment with no time and do not need extra software for its setup.

What we will do?

For this tutorial, we will show you how to set up Ansible on Ubuntu 18.04 Server.

We will install Ansible, setup inventory configuration, setup SSH authentication, and test the connection between the Ansible server and clients.

Step 1 – Install Ansible

First, we will install Ansible to the Ubuntu 18.04 Server. We will add the Ansible PPA repository to the system and install the Ansible package from it.

Add the PPA Ansible repository using the ‘apt-add-repository’ command below.

sudo apt-add-repository ppa:ansible/ansible -y

After that, install the latest version of Ansible using the apt command below.

sudo apt install ansible

Add Ansible Repository for Ubuntu 18.04

Once the installation is complete, check the Ansible version using the following command.

ansible --version

And will get the result as below.

Install Ansible on Ubuntu 18.04

As a result, you’ve successfully installed the latest version of Ansible on Ubuntu 18.04 server.

Step 2 – Setup Ansible Inventory

Ansible inventory is a configuration file that defines hosts and groups of hosts which managed by Ansible. It helps you to manage hosts for multiple environments such as development, staging, and production.

By default, the Ansible has an inventory configuration file on the ‘/etc/ansible’ directory. Also, you can create a custom inventory file for your environment, and call it with the ‘-i’ option on the ansible command.

In this step, we will edit the default inventory configuration and add all managed hosts into it.

Now go to the ‘/etc/ansible’ directory and edit the inventory configuration ‘hosts’ using vim editor.

cd /etc/ansible/
vim hosts

For the single managed server, use the configuration below.

10.5.5.23
10.5.5.24

Define host servers as a group using named ‘servers’ using the following configuration.

For the group of servers, create a new group using the bracket with the name of the group. Then for the next line, type details of managed servers.

[servers]
node01 ansible_host=10.5.5.21
node02 ansible_host=10.5.5.22

Next, we will define a new variable ‘ansible_python_interpreter’ for all hosts on the inventory file. This variable is used for defining the python binary path on every host.

[all:vars]
ansible_python_interpreter=/usr/bin/python3

Save and close.

Next, check all hosts on the ansible inventory using the ‘ansible-inventory’ command below.

ansible-inventory --list

And you will get the result as below.

{
    "_meta": {
        "hostvars": {
            "10.5.5.23": {
                "ansible_python_interpreter": "/usr/bin/python3"
            }, 
            "10.5.5.24": {
                "ansible_python_interpreter": "/usr/bin/python3"
            }, 
            "node01": {
                "ansible_host": "10.5.5.21", 
                "ansible_python_interpreter": "/usr/bin/python3"
            }, 
            "node02": {
                "ansible_host": "10.5.5.22", 
                "ansible_python_interpreter": "/usr/bin/python3"
            }
        }
    }, 
    "all": {
        "children": [
            "servers", 
            "ungrouped"
        ]
    }, 
    "servers": {
        "hosts": [
            "node01", 
            "node02"
        ]
    }, 
    "ungrouped": {
        "hosts": [
            "10.5.5.23", 
            "10.5.5.24"
        ]
    }
}

For the simple and readable list, you can use the ‘–graph’ option as below.

ansible-inventory --graph

And below is the result.

@all:
  |[email protected]:
  |  |--node01
  |  |--node02
  |[email protected]:
  |  |--10.5.5.23
  |  |--10.5.5.24

As a result, you’ve successfully set up a simple Ansible inventory.

Step 3 – Setup SSH Authentication for Ansible

The Ansible uses the SSH service for managing hosts. And you can the SSH service with password-based authentication or using the key-based authentication.

For this guide, we will use the SSH with key-based authentication.

In this step, we will generate the SSH key on the Ansible node and upload the public key to all managed hosts.

Generate the SSH key on the Ansible node using the command below.

ssh-keygen

As a result, the SSH public and private key have been generated to the “~/.ssh/” directory.

Generate SSH Key

Next, we will upload the public key to all managed servers using the ‘ssh-copy-id’ command as below.

ssh-copy-id [email protected]
ssh-copy-id [email protected]

Upload SSH Key to Hosts

Once the public key uploaded to all managed hosts, you can connect to each host without a password.

ssh [email protected]
ssh [email protected]

As a result, you’ve successfully set up SSH using key-based authentication.

Testing SSH Key-Based Authentication

Step 4 – Test Ansible Connection

After configuring the SSH key-based authentication, we will test our configuration from using the ansible command.

We will test to ping all managed hosts and make sure all hosts replying with the pong message.

Test using the ansible command below.

ansible all -m ping -u root

And you will get the result as below.

Ansible Test Ping to All Managed Hosts

As a result, you get the ‘pong’ message from all managed hosts. And you’ve successfully setup Ansible hosts using the key-based authentication.

Step 5 – Ansible Ad-Hoc Command

In this step, we will test the ansible setup using an ‘Ad-Hoc’ command.

The Ansible ‘ad-hoc’ command is used for checking the host directly using the one line Ansible command. It can be used to check all managed hosts by Ansible, and check a single-host server.

– Using Ad-Hoc Command for all managed hosts

To run ansible command for all hosts, you can use the ‘all’ option as below.

ansible all -a "hostname -f" -u root
ansible all -a "df -h" -u root

Below is the result.

Ansible ad-hoc command

As a result, you get the hostname and detail disk usage of all managed servers.

– Using Ad-Hoc command for the specific host

For a specific host, you can use the name of the host defined at the inventory configuration.

ansible node01 -a "hostname -f" -u root
ansible node02 -a "df -h" -u root

Now you will get the hostname of ‘node01’ server and get the details disk of the ‘node02’ server.

Ansible Ad-Hoc Command for Specific Host

Finally, you’ve successfully installed the Ansible on Ubuntu 18.04 Server.

 

Write A Comment