How to Install Ansible on Ubuntu 18.04/16.04 LTS

In this article, We are going to perform How to Install Ansible on Ubuntu 18.04/16.04 LTS or any other cloud platform like Amazon EC2, Azure VM, Google Cloud Compute,etc. with preinstalled Ubuntu 18.04/16.04 LTS.

Introduction

Ansible is an open source configuration management tool. Ansible can help you with configuration management, application deployment, task automation and IT Infrastructure orchestration.

Ansible uses configuration files called playbooks and roles for a series of tasks. The playbooks and roles are written in YAML.

Prerequisites

  • 2 Ubuntu 18.04/16.04 LTS with Minimal Installation
  • SSH Access with sudo privilege

update the system packages

$ sudo apt-get update

Install Ansible on Ubuntu

First Install Required packages to install Ansible.

$ sudo apt install software-properties-common

Add the ansible repository via PPA

$ sudo apt-add-repository --yes --update ppa:ansible/ansible

Update the repository and Install Ansible using below command

$ sudo apt-get update -y
$ sudo apt install ansible

Once Installed Check Ansible version using below command,

$ sudo ansible –version

Output:

  ansible 2.9.4

  config file = /etc/ansible/ansible.cfg

  configured module search path = [u'/home/fosstechnix/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

  ansible python module location = /usr/lib/python2.7/dist-packages/ansible

  executable location = /usr/bin/ansible

  python version = 2.7.15+ 

Configuration of Ansible Master Node

Once Ansible installed on master node change in configuration file as shown below. Remove the # and here I am changing of inventory,remote_tmp and local_tmp location as per my requirement.

$ sudo nano /etc/ansible/ansible.cfg
/etc/ansible/ansible.cfg
# config file for ansible -- https://ansible.com/
# ===============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[defaults]

# some basic default values...

inventory = /home/fosstechnix/ansible/hosts
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/
remote_tmp = /tmp
local_tmp = /tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks = 5
#poll_interval = 15
#sudo_user = root
#ask_sudo_pass = True
#ask_pass = True
#transport = smart
#remote_port = 22
#module_lang = C
#module_set_locale = False

Creating Inventory File

We have to add client system information such as host name, IP address those you want to manage. To add inventory you can create new directory or add in default ansible hosts file.

$ sudo nano /etc/ansible/hosts

OR

$ HOME/ansible/hosts
/etc/ansible/hosts
[WebDev]

WebDev-APP-01  ansible_host=192.168.100.51

Configure and Exchange SSH Key

Ansible is a client less configuration management tool, To communicate with client we have to genrate SSH key on master node and exchange with Slave/Client Systems.

Genrate SSH key on Master node using below command and hit Enter,

$ ssh-keygen

Output:

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/fosstechnix/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/fosstechnix/.ssh/id_rsa.
Your public key has been saved in /home/fosstechnix/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ekun8Aq0a8JpPg13jynucR0+GXovkGpayJAC7Adr3EE [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| E                              |
|. .                              |
|.o .                            |
|+.+ .                         |
|+= o. .S                   |
|oooo..+= +             |
| .o==o==O .           |
| *.*=o*o*                 |
| ooO+...+..               |
+----[SHA256]-----+

Next, Copy generated public key into client system

Syntax:

$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]_system_IP

Example:

$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

Output:

$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/webdev/.ssh/id_rsa.pub"

The authenticity of host '192.168.100.51 (192.168.100.51)' can't be established.

ECDSA key fingerprint is SHA256:cbmgFtO56QLppxNlkj4bdWDOI56uMocOj0ZUeowt7YA.

Are you sure you want to continue connecting (yes/no)? yes

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

[email protected]192.168.100.51's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"

and check to make sure that only the key(s) you wanted were added.

Testing Ansible

Now test the ansible to if the SSH key is exchanged and test the connectivity from master to slave node using PING module.

$ ansible -m ping WebDev

OR

$ ansible all -m ping

Output:

WebDev | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/bin/python"

    },

    "changed": false,

    "ping": "pong"

}

Finally, We have successfully performed Install Ansible on Ubuntu.

Conclusion

In this article, We have Covered How to Install Ansible on Ubuntu, Configure ansible, exchanged SSH keys from master to slave node and Tested the ansible setup.

Related Articles

How to Install Netdata on CentOS 7

Reference:

https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

Sivasai Sagar

I am working as DevOps Engineer and having 5 years of Experience. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Shares
Share via
Copy link