Ansible is an Infrastructure as Code tool that allows its users to control many servers from a centralized location. The benefit of using Ansible is that it uses SSH along with YAML files for configuration without any need to require other configurations. In this tutorial, I will describe how to install and configure Ansible on an Ubuntu 20.04 system.
Installation of Ansible on Ubuntu
To install Ansible, use the official Ubuntu 20.04 repository in your system. Open up the terminal window using the Ctl+Alt+T shortcut or use Applications-> Terminal. After this, update the repository using:
$ sudo apt update
The apt package repository cache will then be updated. Now, install Ansible using:
$ sudo apt install ansible
The system will prompt after a while, press Y from the keyboard, and then hit the enter key to continue.
Ansible will then be installed. Let’s verify this step by using:
$ ansible –version
The version installed will be displayed like this:
Generate an SSH Key
Now, you need to generate an SSH key on your system where Ansible is being installed. To generate the key, append the command:
Once entered in the terminal window, press the enter key.
Again press <enter>
Now, again you need to hit the <enter> key from your keyboard
As soon as you press enter, an output quite similar to this one will be displayed. It will have an SSH key that will be used in the next half of the tutorial.
Configuration of Ubuntu hosts to automate Ansible
To automate more than one host, you need to repeat the same process for each of the hosts respectively. All Ubuntu hosts (Ansible) which are to be configured must have the SSH package installed. Now, we will update the apt package using:
$ sudo apt update
The updates will begin quite similar to the one displayed below:
Next step is to install OpenSSH server using:
$ sudo apt install openssh-server -y
Once done, then you need to check the status of sshd service. Use the following command to check this:
$ sudo systemctl status sshd
The output will be displayed as soon as you press <enter>. The statuses enabled and active (running) will
Once you have checked that sshd command is running fine and enabled, then you can proceed. If not enabled then start it manually by using:
$ sudo systemctl start sshd
Now, let’s configure the firewall to allow SSH access by using:
$ sudo ufw allow ssh
An output similar to the one displayed below will appear:
The next step is to add an ansible user and then allow password-less access. We will now create an ansible user by using:
$ sudo adduser ansible
Provide the password for the ansible user.
After that fill in the relevant information against all available fields.
To configure the password-less sudo access type the following in the terminal window for your ansible user:
$ echo "ansible ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/ansible
Let us check out the IP address of the Ansible host by using:
$ hostname -I
The host will appear in the output.
Now, you know the hostname so, we will be copying the SSH public key to the Ansible host like this:
$ ssh-copy-id [email protected]
Now, type Yes to proceed.
The key will be copied to the host.
To disable password-based login use the command:
$ sudo usermod -L ansible
Here we are able to access the Ansible host without any password and it is ready for automation.
To test Ansible after the installation and configuration, users need to create a new project directory. Use the mkdir command to do so:
$ mkdir ~/ansible-demo
Once, you have created new directory, you need to access it using:
$ cd ~/ansible-demo/
After that, create a new host file in the same project directory using:
Ansible will be using the hosts in this file to SSH. Once you have opened the nano editor, type the IP address of your host, you want to ping.
Let’s try to ping all hosts using Ansible by using:
$ ansible all -i ./hosts -u ansible -m ping
You will see a success like this one displayed below:
Uninstallation of Ansible
To uninstall Ansible, use the following command in the terminal window:
$ sudo apt remove ansible
Type Y to proceed with the uninstallation process.
This way users can easily remove Ansible from their system.
In this article, we saw the Ansible installation process on an Ubuntu 20.04 system and how to configure and test Ansible.