Easy steps to install ansible command line on Ubuntu 18.04

ComputerCarriage > Posts > Ansible > Easy steps to install ansible command line on Ubuntu 18.04
Easy steps to install ansible command line

Ansible command line installation we are going perform in this post. After this you can able to install and configure the ansible control and managed nodes.

1. Ansible command Line – Introduction

Ansible is an open source tool used to automate the daily tasks and avoid the repeated tasks, Ansible works on python and ssh protocol to communicate with the nodes. Ansible works on Control / Managed nodes, Control node is an Ansible node and client nodes are called managed nodes. We can use any servers as an Ansible control node, According to the Requirements we can configure Ansible control node.

2. Advantages of Ansible

Few advantages are listed below

  • Ansible is open source tool used to automizing the day to day tasks
  • Ansible jobs execute remotely in Linux distributions, Windows
  • Ansible automation tools used for software provision, configuration management, application deployment.
  • Ansible is agent less tools and it is execute via the ssh protocol.
  • Ansible playbooks can be executed with or without password depending upon the how we need to use it

In this chapter we are going to see how to install the ansible in ubuntu
Before installing the ansible package always install the latest patch update to the current OS, Ansible installed machine is called the control node and clients are managed nodes.

3. Update to latest repository

In this installation we are going to use pre-installed Ubuntu 18.04 server, update the repository to latest

devops@devops:~$ sudo apt-get update
Hit:1 http://in.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu bionic-security InRelease
Reading package lists… Done
devops@devops:~$

4. Update the latest package

Before starting update the latest package in the control node

devops@devops:~$ sudo apt-get upgrade -y
Reading package lists… Done
Building dependency tree
Reading state information… Done
Calculating upgrade… Done
The following packages will be upgraded:
bsdutils distro-info-data dmidecode fdisk kmod landscape-common libblkid1 libfdisk1
libkmod2 libmount1 libnss-systemd libpam-systemd libsmartcols1 libsystemd0 libudev1
libuuid1 lxcfs mount open-vm-tools python3-update-manager sosreport systemd systemd-sysv
udev unattended-upgrades update-manager-core util-linux uuid-runtime
28 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 7344 kB of archives.
After this operation, 238 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 bsdutils amd64 1:2.31.1-0.4ubuntu3.6 [60.3 kB]
----------------------Output truncated ---------------------
Processing triggers for mime-support (3.60ubuntu1) …
Processing triggers for initramfs-tools (0.130ubuntu3.9) …
update-initramfs: Generating /boot/initrd.img-4.15.0-99-generic
devops@devops:~$

5. Add the Ansible repo

Add the ansible repo, this enable the ansible package to install

devops@devops:~$ sudo apt-add-repository ppa:ansible/ansible
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.
http://ansible.com/
More info: https://launchpad.net/~ansible/+archive/ubuntu/ansible
Press [ENTER] to continue or Ctrl-c to cancel adding it.
Hit:1 http://in.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic InRelease [15.9 kB]
Get:3 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main amd64 Packages [692 B]
Hit:4 http://in.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:5 http://in.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:6 http://in.archive.ubuntu.com/ubuntu bionic-security InRelease
Get:7 http://ppa.launchpad.net/ansible/ansible/ubuntu bionic/main Translation-en [472 B]
Fetched 17.0 kB in 2s (7480 B/s)
Reading package lists… Done

6. List the ansible available package

List the available ansible package in the repo, We could see two ansible versions are present, latest 2.9.7 and older version is 2.5.1, as of we are going to install the latest ansible version

devops@devops:~$ apt list -a ansible
Listing… Done
ansible/bionic 2.9.7-1ppa~bionic all [residual-config]
ansible/bionic-updates,bionic-security,now 2.5.1+dfsg-1ubuntu0.1 all [residual-config]
ansible/bionic 2.5.1+dfsg-1 all [residual-config]

7. Install the ansible package

Using apt-get command install the ansible latest package

devops@devops:~$ sudo apt-get install ansible
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following additional packages will be installed:
ieee-data libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-asn1crypto python-certifi python-cffi-backend python-chardet
ansible ieee-data libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-asn1crypto python-certifi python-cffi-backend python-chardet
python-crypto python-cryptography python-enum34 python-httplib2 python-idna python-ipaddress python-jinja2 python-jmespath python-kerberos
python-libcloud python-lockfile python-markupsafe python-minimal python-netaddr python-openssl python-paramiko python-pkg-resources python-pyasn1
python-requests python-selinux python-simplejson python-six python-urllib3 python-xmltodict python-yaml python2.7 python2.7-minimal
0 upgraded, 37 newly installed, 0 to remove and 0 not upgraded.
Need to get 12.1 MB of archives.
After this operation, 79.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
----------------------Output truncated ---------------------
Processing triggers for mime-support (3.60ubuntu1) …
Processing triggers for man-db (2.8.3-2ubuntu0.1) …
devops@devops:~$

Note:

By default python package will be installed along with Operating system

8. Verify the Installation

Verify the installation and also check the latest version has been installed

devops@devops:~$ ansible --version
ansible 2.9.7
config file = None
configured module search path = [u'/home/devops/.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.17 (default, Apr 15 2020, 17:20:14) [GCC 7.5.0]
devops@devops:~$

Now we are good to proceed for the configuration

9. Pre-Configuration

Before starting check we need to do some configurations

  • First we need to get the ssh-key for the ansible installed node
  • Generate ssh-key and copy ssh keys to managed nodes user

Generate ssh-keys for the control node

In this example the hostname of the ansible server is control and server1, server2, server3.. as managed nodes.

On Control Node just press enter with default values to generate ssh-keys

devops@devops:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/devops/.ssh/id_rsa):
Created directory '/home/devops/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/devops/.ssh/id_rsa.
Your public key has been saved in /home/devops/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:GKUr/N2DWoHOmQh8rSwo3bJVZHRP76rT7UOq4/Kq4sY devops@devops
The key's randomart image is:
+---[RSA 2048]----+
| . o . |
| . + o . |
| = . . |
| . . + = . |
| o + * S . |
| o = O + + .. |
|o.+ * * +.++ |
|. E= .oo.o.o |
| oo…o==+ … |
+----[SHA256]-----+
devops@devops:~$

Verify the keys

Verify the private and public keys are created

devops@devops:~$ ls -l .ssh/
total 8
-rw------- 1 devops devops 1675 May 4 10:42 id_rsa
-rw-r--r-- 1 devops devops 395 May 4 10:42 id_rsa.pub
devops@devops:~$

Configure Managed Nodes

After the successful creation of ssh-keys on the control node, copy the public keys to the managed nodes

Copy the id_rsa.pub key to the managed nodes(server2) by the following command

devops@devops:~$ ssh-copy-id devops@server2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/devops/.ssh/id_rsa.pub"
The authenticity of host 'server2 (192.168.43.152)' can't be established.
ECDSA key fingerprint is SHA256:CyMsiRb/wDJheI1pan4pP17ubs+XKDsKPRWEqks1hGI.
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
devops@server2's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'devops@server2'"
and check to make sure that only the key(s) you wanted were added.

Verify the keys are added in managed nodes

devops@server2:~$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqmh6eLJzoSsCQNVydsiCZhuKk6F7fkRi7kxZhJrnEsgcNyfkYAJIrp+gFURKJ2wp3Uvw90biX/rifd1mS+7bSKrEnfjdnTB2g4WE0xioMJRhUU90EfrHbcqGVuRYuVFBhiMhux0Znx0pt7ZGWyCLZZ2vkCqgpuMOD0MzcQTr2S/4FZGiRnIRrPLaItOfIXF6xmNAGaIVlVMPgpUyTFuL/ZbyBk3nRXE34DOfVuJFOU4puFfk+I/eP5N5oGcem3vgMPV6PjvgnuPRLKJQ+d/ku3O/x0nn+DkJ7Io6iK6BMGvjnM6nxJ4u2HA89HajJc/lSYdhcX20oKn/CxYYu0mn/ devops@devops
devops@server2:~$

SUDO user add

Now add the managed node user to sudo privilege

echo “devops ALL=(ALL) NOPASSWD:ALL”

Verify the user has been added in the sudo file

devops@server2:~$ sudo grep devops /etc/sudoers
devops ALL=(ALL) NOPASSWD:ALL

Note:

do not use root user as an ansible, use any user with special privileges

10. Setting up Managed nodes

Leaving the default configuration for /etc/ansible/ansbile.cfg, adding the hosts details for the ansible control to execute jobs

The ansible configuration file is /etc/ansible/ansible.cfg which is part of the ansible package. the host file or inventory file is present in /etc/ansible/hosts.

sudo nano /etc/ansible/hosts
server2

11. Verify the Configuration

Using simple ping module we are going to test the ansible configuration, whether control and managed are able to communicate each other

devops@devops:~$ ansible server2 -m ping
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host server2 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with
prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
server2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
devops@devops:~$

You could able to see the result pong.

12. Summary

Hope you have understood how to install the ansible, and add ssh-keys and create ansible users and inventory. We can see in depth of ansible in coming posts. let us know your inputs, we can get it done.

Also Refer

Ansible Playbook Howto https://computercarriage.com/2020/05/21/ansible-playbook-for-package/

Fore more info check https://docs.ansible.com

One thought on “Easy steps to install ansible command line on Ubuntu 18.04

Leave a Reply