How to Install Ansible on Ubuntu 20.04
Ansible is a popular open-source automation tool that allows you to manage and configure servers, applications, and networks. In this tutorial, we will show you how to install Ansible on Ubuntu 20.04 and get started with basic usage.
Prerequisites
Before you begin, you should have a non-root user account with sudo privileges on your Ubuntu 20.04 server.
Installing Ansible using apt-get
To install Ansible on Ubuntu 20.04, follow these steps:
Update your package index and install the required dependencies:
sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
Install ansible with:
sudo apt install ansible
To verify the Ansible installation you can use:
ansible --version
The output should look similar to this:
ansible 2.9.1
config file = /etc/ansible/ansible.cfg
This show us that Ansible is indeed correctly installed.
Generate SSH keys and share it among managed nodes
After the installation is complete we can set up SSH Keys for ansible. Using SSH Keys for Ansible is recommended and allows us to not enter a password for each connection plus it adds an extra layer of security.
To generate a SSH Key, we can run the following command:
ssh-keygen -o -a 100 -t ed25519 -C "[email protected]"
This will create a ED25519 key. This is the most recommended assymetric key algorithm today!
Copy the public key to the managed nodes:
ssh-copy-id username@remote_host
Test the connection with:
ssh username@remote_host
Close the connection.
Setting Up the Inventory File
Ansible uses an inventory file to define the managed nodes and their properties. To set up the inventory file, follow these steps:
Create a new file named hosts in /etc/ansible/ directory:
sudo nano /etc/ansible/hosts
Add the IP addresses or hostnames of your managed nodes to the file, for example:
[webserver]
192.168.1.10
Save and close the file.
Running Ad-Hoc Commands
You can use Ansible to run ad-hoc commands on your managed nodes. For example, to check the uptime of the managed nodes, run the following command:
ansible all -a uptime
Create ansible cfg and inventory file
If you prefer to use a different inventory file or change the default settings of Ansible, you can create a configuration file named ansible.cfg in your working directory. You can also create a custom inventory file and specify it in the ansible.cfg file.
Create a Demo Ansible playbook
Ansible uses playbooks to define the tasks and workflows that should be executed on the managed nodes. To create a demo Ansible playbook, follow these steps:
Create a new file named demo.yml in your working directory:
nano demo.yml
Add the following content to the file:
- hosts: webserver
tasks:
- name: Install Nginx
apt: name=nginx state=present
To run the playbook you type:
ansible-playbook demo.yml
Conclusion
In this tutorial, we have shown you how to install Ansible on Ubuntu 20.04 and get started with basic usage. Ansible is a powerful automation tool that can help you save time and effort in managing your infrastructure. We encourage you to explore the advanced features of Ansible and create your own playbooks and modules.