How to Install Ansible on Ubuntu 18.04

Install Ansible on Ubuntu 18.04

In this article, we will have explained the necessary steps to install and configure Ansible on Ubuntu 18.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Ansible is a free and open-source Configuration and automation tool for UNIX like operating system. It is written in python and similar to Chef or Puppet but there is one difference and advantage of Ansible is that we don’t need to install any agent on the nodes. It uses SSH for making communication to its nodes.

In this article we will demonstrate how to install and configure Ansible in Ubuntu and will try to manage its two nodes:

  • Ansible Server – ansible.idroot.us ( 192.168.13.33 )
  • Ansible Client – 192.168.100.120

Install Ansible on Ubuntu

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install Ansible on Ubuntu.

We need to enable the ansible repo first. Install the repo with the following command:

sudo apt-add-repository ppa:ansible/ansible

After adding the Ansible repository execute below commands to install:

sudo apt update
sudo apt install ansible

Upon successful installation of Ansible, you can verify the version of ansible installed by running:

sudo ansible --version

Step 3. Setup Password less SSH configuration between the ansible server and ansible client.

We need to generate ssh keys on the ansible server and copy this key to public key on ansible client:

[root@idroot ~]# ssh-keygen -t rsa -b 4096 -C "root@192.168.13.33"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:rqAwbwQ8/meilanamaria4D2V0fQ49amw2WKbYIA root@192.168.13.33
The key's randomart image is:
+---[RSA 4096]----+
| .oo.+o |
| . =.=.. . |
|. E o + X . o |
|.o. + + = X . |
| .o. + oSX + |
| .. o.+ = |
|o. o oo.o |
| +.. +oo |
| .o ... |
+----[SHA256]-----+
[root@idroot ~]#

We can use ssh-copy-id command to copy the public key from the ansible server to ansible clients:

[root@idroot ~]# ssh-copy-id root@192.168.13.33
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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
root@192.168.43.15's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@192.168.13.33'"
and check to make sure that only the key(s) you wanted were added.

[root@idroot ~]#
[root@idroot ~]# ssh-copy-id root@192.168.100.120
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.43.131 (192.168.43.131)' can't be established.
ECDSA key fingerprint is SHA256:OuHrnRxppengenkimpoi5Z+1tOWOF8eYZ5Le5MNwUQ.
ECDSA key fingerprint is MD5:78:1c:a5:72:bb:25:fa:c7:67:39:fc:91:b9:fb:b6:20.
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
root@192.168.43.131's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@192.168.100.120'"
and check to make sure that only the key(s) you wanted were added.

[root@idroot ~]#
===============================
[root@urclouds ~]# ssh root@192.168.100.120
Last login: Thu Sep 19 16:00:57 2019 from bezafari-c1
[root@client ~]# exit
logout
Connection to 192.168.100.120 closed.
[root@idroot ~]#

We need to check our ansible server and ansible clients is working password less or not? We can check as like below:

[root@idroot ~]# ssh root@192.168.100.120
Last login: Thu Jan 17 17:00:57 2019 from bezafari-c1
[root@client ~]# exit
logout
Connection to 192.168.100.120 closed.
[root@idroot ~]#

Step 4. Ansible server configuration to manage ansible client.

You can make inventory of our ansible server and ansible client using ansible server host file. Host file can be find in this path on ansible server /etc/ansible/host:

[root@idroot ~]# nano /etc/ansible/hosts
[test-servers]
192.168.13.33
192.168.100.120

Step 5. Manage ansible client from ansible server.

Check the connectivity of ‘test-servers’ or ansible clients using ping:

[root@idroot ~]# ansible -m ping 'test-servers'
192.168.13.33 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.100.120 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@idroot ~]#

We can check the uptime of our ansible client from ansible server:

[root@idroot ~]# ansible -m command -a "uptime" 'test-servers'
192.168.100.120 | CHANGED | rc=0 >>
16:17:35 up 20 min, 3 users, load average: 0.09, 0.16, 0.29

192.168.13.33 | CHANGED | rc=0 >>
16:17:35 up 22 min, 4 users, load average: 0.18, 0.29, 0.55

[root@idroot ~]#

Redirecting the output of the command to a file. Like below:

[root@idroot ~]# ansible -m command -a "df -Th" 'test-servers' > /tmp/command-output.txt
[root@idroot ~]# cat /tmp/command-output.txt
192.168.13.33 | CHANGED | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 13G 5.0G 8.0G 39% /
devtmpfs devtmpfs 482M 0 482M 0% /dev
tmpfs tmpfs 497M 212K 497M 1% /dev/shm
tmpfs tmpfs 497M 7.1M 490M 2% /run
tmpfs tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 158M 340M 32% /boot
tmpfs tmpfs 100M 16K 100M 1% /run/user/0
192.168.100.120 | CHANGED | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 13G 3.9G 9.1G 30% /
devtmpfs devtmpfs 906M 0 906M 0% /dev
tmpfs tmpfs 921M 84K 920M 1% /dev/shm
tmpfs tmpfs 921M 8.7M 912M 1% /run
tmpfs tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 157M 341M 32% /boot
tmpfs tmpfs 185M 20K 184M 1% /run/user/42
tmpfs tmpfs 185M 0 185M 0% /run/user/0
[root@idroot ~]#

That’s all you need to do to install Ansible on Ubuntu 18.04. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.