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.
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:
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:
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.
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:
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:
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).
#
#
# / 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
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:
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.
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.