How to Install Ansible on Ubuntu 20.04 Server (Control Node)

How to Install Ansible on Ubuntu 20.04 Server.  If you’re a system administrator and managing hundreds or thousands of servers then it is a very time-consuming process for you to manage all servers. This is where Ansible comes into the picture.

Ansible is a free, open-source, and next-gen configuration management and provisioning tool. It is very similar to other configuration management tools like Chef, Puppet, and Salt. It lets you control and configure one or more remote servers from a single machine. It uses SSH protocol to connect to servers and execute the configured tasks to all servers. Ansible helps system administrators to automate tasks that are either cumbersome or repetitive or complex.

Install Ansible on Ubuntu 20.04 server
Ansible Architecture

Compared to other configuration management tools, Ansible is agent-less. This means you don’t need to install any agent on the remote server. Ansible Server uses a YAML language that allows you to define all jobs in a YAML file and Ansible lets run all jobs using a single command.

Table of Contents

Important Terms in Ansible

  • Ansible Control Node – The machine where Ansible is installed. It is responsible for running and managing all servers.
  • Ansible Managed Node – List of all servers that you want to manage.
  • Inventory – It contains all configuration information of the server that you want to manage.
  • Playbook – A YAML file where you can define all jobs through tasks using YAML format.
  • Modules – Ansible comes with a lot of modules used for performing different tasks.

We will use the following setup for the Ansible demonstration:

Setup SSH Key-based Authentication

Ansible uses SSH to connect to each remote server and execute tasks. So you will need to set up an SSH key-based authentication between Ansible control node and manage nodes.

On the Ansible controller node, generate an SSH key pair using the following command:

				
					ssh-keygen -t rsa
				
			

Provide empty passphrash and hit Enter. You should see the following output:

				
					Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory 'https://net.cloudinfrastructureservices.co.uk/root/.ssh'.
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:row4sQrcj2cnYq4Ma1W8wRjq8ewbbsm2tmkLtg2rnmo root@ansible
The key's randomart image is:
+---[RSA 3072]----+
| |
| . |
| . = |
| o . = |
|. + . o S |
|..o+ . . |
|o=+=. . |
|=E@%=* o |
|@OX%X.= |
+----[SHA256]-----+
				
			

You can verify the generated SSH key using the following command:

				
					ls -l ~/.ssh/id_rsa*
				
			

Sample output:

				
					-rw------- 1 root root 2602 Sep 12 09:58 /root/.ssh/id_rsa
-rw-r--r-- 1 root root 566 Sep 12 09:58 /root/.ssh/id_rsa.pub
				
			

Next, you will need to copy the public key to both remote hosts:

				
					ssh-copy-id root@172.16.0.11
ssh-copy-id root@172.16.0.12
				
			

Next, verify whether you can connect to remote hosts without providing an SSH password:

				
					ssh root@172.16.0.11
ssh root@172.16.0.12
				
			

If everything is fine, you can log in to remote hosts without providing an SSH password.

Install Ansible on Ubuntu 20.04

Now, you will need to install the Ansible package on the Ansible control node. By default, Ansible is not included in the Ubuntu official repository. So you will need to add the Ansible repository to APT.

You can add it using the following command:

				
					apt-get install gnupg2 software-properties-common -y
add-apt-repository --yes --update ppa:ansible/ansible
				
			

Once the repository is added, update the repository cache and install the Ansible package using the following command:

				
					apt-get update -y
apt-get install ansible -y
				
			

Once the Ansible is installed successfully, verify the Ansible version and configuration file information using the following command:

				
					ansible --version
				
			

Sample output:

				
					ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
				
			

Create an Ansible Inventory File

By default, Ansible default configuration file and inventory file is located at /etc/ansible/ansible.cfg and /etc/ansible/hosts respectively. In this section, we will create both files inside /root/project directory.

  • ansible.cfg is a configuration file to tell how ansible should be running.
  • inventory is a file that stores all configuration information of remote hosts.

First, create a project directory and ansible.cfg file using the following command:

				
					mkdir project
nano project/ansible.cfg
				
			

Add the following lines:

				
					[defaults]

inventory=/root/project/inventory
remote_user=root
host_key_checking=False
become=True
become_user=root
become_ask_pass=False
				
			

Save and close the file then create an inventory file:

				
					nano project/inventory
				
			

Add your Ansible hosts IP address as shown below:

				
					[webserver]
172.16.0.11

[dbserver]
172.16.0.12
				
			

Save and close the file then export the Ansible new configuration path with the following command:

				
					export ANSIBLE_CONFIG=/root/project/ansible.cfg
echo "export ANSIBLE_CONFIG=/root/project/ansible.cfg" >> ~/.profile
source ~/.profile
				
			

Now, change the directory to project and verify the Ansible new configuration path using the following command:

				
					cd project
ansible --version
				
			

You should see the Ansible new configuration path in following output:

				
					ansible 2.9.6
config file = /root/project/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
				
			

Working with Ansible Modules

Ansible comes with a lot of built-in modules that allow you to perform specific tasks on remote hosts via ad-hoc commands. In this section, we will give some practical examples.

Verify Ansible Hosts Connectivity

First, use Ansible’s built-in ping module to run a connectivity test on all remote hosts which you have defined in your inventory file:

				
					ansible -m ping all
				
			

This command will checks if all hosts are accessible and have valid SSH credentials. If everything is fine. You should get the following output:

				
					172.16.0.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
172.16.0.12 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
				
			

Verify Uptime of Ansible Hosts

Next, use the uptime module with a shell flag to run the “uptime” command on all remote hosts.

				
					ansible -m shell -a "uptime" all
				
			

If both remote hosts are up, you should get the following output:

				
					172.16.0.12 | CHANGED | rc=0 >>
10:10:29 up 13 min, 3 users, load average: 0.00, 0.14, 0.14
172.16.0.11 | CHANGED | rc=0 >>
10:10:29 up 13 min, 3 users, load average: 0.00, 0.14, 0.14
				
			

Install and Remove a Package on Ansible Hosts

You can use the apt module to install a specific package on the Ansible hosts. For example, to install a telnet package on the webserver host, run the following command:

				
					ansible -m ansible.builtin.apt -a "name=telnet state=present" webserver
				
			

Once the package is installed successfully, you should get the following output:

				
					172.16.0.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"cache_update_time": 1631441046,
"cache_updated": false,
"changed": true,
...
"Preparing to unpack .../telnet_0.17-41.2build1_amd64.deb ...",
"Unpacking telnet (0.17-41.2build1) ...",
"Setting up telnet (0.17-41.2build1) ...",
"update-alternatives: using /usr/bin/telnet.netkit to provide /usr/bin/telnet (telnet) in auto mode",
"Processing triggers for man-db (2.9.1-1) ..."
]
}
				
			

To install a package named Nmap on the dbserver host, run the following command:

				
					ansible -m ansible.builtin.apt -a "name=nmap state=present" dbserver
				
			

To remove a package named Nmap on the dbserver host, run the following command:

				
					ansible -m ansible.builtin.apt -a "name=nmap state=absent" dbserver
				
			

Display File Content on Ansible Hosts

You can use the shell module to run the “cat /etc/fstab” command on the dbserver host and display its content:

				
					ansible -m shell -a "cat /etc/fstab" dbserver
				
			

Sample output:

				
					172.16.0.12 | CHANGED | rc=0 >>
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda1 during installation
UUID=29a0b164-1ba1-45a7-b23a-cdb98f23edbc / ext4 noatime,errors=remount-ro 0 1
/swapfile none swap sw 0 0
				
			

Create and Remove a Directory on Ansible Hosts

Ansible file module allows you to create, remove and manage the directory on the remote hosts. For example, to create a directory named dir1 with specific permissions on the webserver host, run the following command:

				
					ansible -m ansible.builtin.file -a "dest=/mnt/dir1 mode=755 owner=root group=root state=directory" webserver
				
			

Sample output:

				
					172.16.0.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/mnt/dir1",
"size": 4096,
"state": "directory",
"uid": 0
}
				
			

Now, use the option “state=absent” to remove a directory named dir1 from the webserver host.

				
					ansible -m ansible.builtin.file -a "dest=/mnt/dir1 state=absent" webserver
				
			

Sample output:

				
					172.16.0.11 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"path": "/mnt/dir1",
"state": "absent"
}
				
			

Create Ansible Playbook to Install Packages on Ansible Hosts

The playbook is a file where you can write Ansible code to perform repeatable and configuration management. It is written in YAML format that defines work for a server configuration managed by Ansible.

In this section, we will create a playbook named install.yaml to install Apache and PHP on webserver host and MariaDB on dbserver host.

Let’s create an install.yaml file inside the project directory:

				
					nano project/install.yaml
				
			

Add the following code:

				
					---
- name: Playbook to Install Apache webserver on webserver node.
hosts:
- webserver
tasks:
- name: Install apache and php
package:
name:
- apache2
- php
state: present

- name: Playbook to Install MariaDB database server on dbserver node.
hosts:
- dbserver
tasks:
- name: Install mariadb
package:
name:
- mariadb-server
- mariadb-client
state: present
				
			

Save and close the file. Then, navigate to the project directory and run the Ansible playbook using the following command:

				
					cd project
ansible-playbook install.yaml
				
			

If the playbook has been executed successfully. You should get the following output:

				
					PLAY [Playbook to Install Apache webserver on webserver node.] ********************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************
ok: [172.16.0.11]

TASK [Install apache and php] *****************************************************************************************************************
changed: [172.16.0.11]

PLAY [Playbook to Install MariaDB database server on dbserver node.] **************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************
ok: [172.16.0.12]

TASK [Install mariadb] ************************************************************************************************************************
changed: [172.16.0.12]

PLAY RECAP ************************************************************************************************************************************
172.16.0.11 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.0.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

				
			

To verify the installed package on Ansible hosts, run the following command:

				
					ansible -m shell -a 'dpkg -l | grep -E "apache2"' webserver
ansible -m shell -a 'dpkg -l | grep -E "mariadb"' dbserver
				
			

Install Ansible on Ubuntu 20.04 Server Completed

In this guide, we explained how to install Ansible and setup an inventory file to execute commands from an Ansible control node. We also create and run a playbook to automate package installation. You can now able control remote hosts from the Ansible control node.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x