Ansible is an automation tool for managing multiple remote hosts from the single machine. It provides you with an easy to configure for a large number of remote hosts. For example, you are performing the same tasks over multiple machines, Ansible provides you the option to automate these tasks.
Ansible is a better alternatives of the other popular infrastructure automation tools available like Chef and Puppet. You don’t need to install any client software on nodes to manage through Ansible server. It uses SSH connection to execute tasks on nodes.
This tutorial will help you to install and configure Ansible on Ubuntu 20.04 LTS Linux systems.
Prerequisites
We have one control node to configure Ansible server and three node servers to be managed. Here control node is running with Ubuntu 20.04 Linux system. First and third node is running with Ubuntu 18.04 server and Second node is running with centos 7 server.
Here is list of nodes with IP address and hostnames:
- Control node – 10.0.1.10 (control-node)
- First node – 10.0.1.101 (web-host1)
- Second node – 10.0.1.102 (web-host2)
- Third node – 10.0.1.103 (db-host1)
Step 1 – Setup SSH Keys
You can configure key based ssh for the remote Linux Ansible hosts. So password will not be required for SSH. Ansible also allows you to use a password for ssh, but key-based ssh is more secure.
Login to the control node (10.0.1.10) and generate ssh key pair:
ssh-keygen -t rsa
Just press “Enter” to all the input asked by the command.
Copy the public key to all your remote nodes you need to connect via with SSH protocols.
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
Step 2 – Installing Ansible on Ubuntu
You can install Ansible server from official packages repositories on Ubuntu system. Which has the latest debian packages. Execute the following command to setup Ansible PPA on your Ubuntu system.
sudo apt-add-repository ppa:ansible/ansible
Software Updater utility will update the packages cache on your system. So you have to run the following command to install or update Ansible on your Ubuntu system
sudo apt update
sudo apt install ansible
Enter ‘Y’ for all the installation confirmation to complete install process. Next, you need to configure Ansible server
Step 3 – Configure Inventory File
Your server is ready with Ansible for remote host management and automation. You can have a number of hosts you need and manage them with single Ansible server.
Here you need to define your remote systems in Ansible hosts file (/etc/ansible/hosts). You can also make groups of hosts with similar types. Here you need to properly organize your hosts into groups. Groups are used for performing one task on all remote hosts defined under it.
Edit Ansible hosts configuration file. For exmaple:
sudo nano /etc/ansible/hosts
Add your hosts and organize them with groups. A host can be added under multiple groups.
[webservers] web-host1 web-host2 [dbservers] db-host1
The below image will help you to understand group and hosts under a group.
Single Host Vars Configuration
You need to define settings for your hosts. The host-specific file must be with the same name as host (eg: web-host1) under the host_vars directory.
sudo mkdir /etc/ansible/host_vars/
sudo vi /etc/ansible/host_vars/web-host1
Add the SSH settings to this file for the web-host1.
ansible_ssh_host: 10.0.1.101 ansible_ssh_port: 22 ansible_ssh_user: root
In case you don’t have used Step 1 for the ssh connection for this host. You can also add one of the below methods to web-hosts1 configuration file for the authentication.
ansible_ssh_pass: secret_password ansible_ssh_private_key_file: /home/rahul/.ssh/aws.pem
Group Vars Configuration
You can configure common variable settings of a Group under group configurations. The group file name must be same as the group name (eg: webservers) under group_vars
directory.
sudo mkdir /etc/ansible/group_vars
sudo vi /etc/ansible/group_vars/webservers
Add the common variables to this file used by all the hosts added under this group.
ansible_ssh_port: 22 ansible_ssh_user: root
Step 4 – Testing Ansible Connection
Your Ansible is ready to use. To test all nodes connectivity use ping module. Login to your Ansible server and execute following command:
ansible -m ping all
You can also test connectivity for the specific host or groups.
ansible -m ping web-host1
## Specific host ansible -m ping webservers
## Specific group
You can also run any Linux command using the Ansible shell module. For example, execute the below command to test the free memory on web-host1.
ansible -m shell -a 'free -m' web-host1
You can also perform the same task for a group. Just use group name instead of hostname.
Conclusion
In this tutorial, you have learned to install Ansible server on Ubuntu 20.04 system. Also configured remote hosts to be managed with Ansible server.