Network Topology:
Here, the linuxhint-711ea is an Ubuntu 20.04 LTS machine where I will install Ansible.
Then, I will configure the hosts host1 (IP address 192.168.20.162) and host2 (IP address 192.168.20.153) for Ansible automation and run commands on them using Ansible from the linuxhint-711ea machine.
I will simply call host1 and host2 as Ansible hosts in this article.
Installing Ansible:
You can easily install Ansible on Ubuntu 20.04 LTS from the official package repository of Ubuntu.
First, update the APT package repository cache with the following command:
The APT package repository cache should be updated.
Now, install Ansible with the following command:
To confirm the installation, press Y and then press <Enter>.
Ansible should be installed.
Now, run the following command to check if ansible is working correctly.
As you can see, ansible command is available and working correctly.
Generating SSH Key:
Now, you have to generate an SSH key on the computer where you have installed Ansible.
To generate an SSH key, run the following command:
Now, press <Enter>.
Press <Enter>.
Press <Enter>.
An SSH key should be generated.
Configuring Ubuntu Hosts for Ansible Automation:
In this section, I will show you how to configure an Ubuntu host (host1) for Ansible automation. If you have more than one host which you want to automate using Ansible, then repeat the same process on each of the hosts.
The Ubuntu Ansible hosts (which you want to configure for Ansible automation) must have SSH server package installed.
First, update the APT package repository cache with the following command:
Then, install OpenSSH server with the following command:
In my case, the OpenSSH server package is already installed. If it is not installed in your case, it should be installed.
Now, check if the sshd service is running with the following command:
As you can see, the sshd service is active (running) and enabled (will automatically start on system boot).
If the sshd service is not active (running) in your case, start it manually with the following command:
If the sshd service is not enabled (not added to the system startup) in your case, add it to the system startup manually with the following command:
Now, configure the firewall to allow SSH access with the following command:
You should also create an ansible user and allow password-less sudo access to the ansible user.
To create an ansible user, run the following command:
Now, type in a password for the ansible user and press <Enter>.
Now, retype the password and press <Enter>.
Now, press <Enter>.
Now, press <Enter>.
Now, press <Enter>.
Now, press <Enter>.
Now, press <Enter>.
Now, type in y and then press <Enter>.
An ansible user should be created.
Now, configure password-less sudo access to the ansible user with the following command:
Now, find the IP address of the Ansible host (host1) with the following command:
Here, the IP address in my case is 192.168.20.162. It will be different for you. So, make sure to replace it with yours form now on.
Now, from the computer where you have installed Ansible, copy the SSH public key to the Ansible host (host1) as follows:
Type in yes and press <Enter>.
Now, type in the password for the ansible user and press <Enter>.
The public SSH key should be copied to host1.
Now, disable password-based login for the ansible user with the following command:
Now, you can only SSH into the Ansible host (host1) as ansible user without any password from the computer you have copied the SSH public key from (in this case, the computer where you have installed Ansible). But you won’t be able SSH into the Ansible host (host1) as ansible user from any other computer. I have configured the Ansible hosts this way for security reasons. As the ansible user don’t need any password for running administrative commands, it’s risky to allow password-based login for the ansible user.
Now, you should be able to SSH into the ansible host host1 from the computer where you have installed Ansible as follows:
As you can see, I am able to access the Ansible host (host1) as ansible user without any password. So, the Ansible host (host1) is ready for Ansible automation.
If for some reason, you want to allow password-based login for the ansible user again, run the following command in the Ansible host (host1):
You can configure as many hosts as you want for Ansible automation the same way.
In this article, I have configured only 2 hosts, host1 and host2 for the demonstration.
Testing Ansible:
Now, create a new project directory ~/ansible-demo/ in the computer where you have installed Ansible as follows:
Now, navigate to the ~/ansible-demo/ directory as follows:
Now, create a new hosts file in the project directory as follows:
Now, type in the IP addresses or DNS names of the Ansible hosts (host1 and host2 in my case) in the hosts file as follows:
192.168.20.153
Now, save the file by pressing
Now, try to ping all the hosts using Ansible as follows:
NOTE: Here, -u option is used to specify the username (ansible in this case) which Ansible will use to SSH into the hosts.
As you can see, all the hosts can be pinged. So, the hosts are ready for Ansible automation.
The same way, you can run any command in the hosts using Ansible as follows:
As you can see, the command ran successfully in each of the hosts and the output is displayed.
So, this is how you install Ansible on Ubuntu 20.04 LTS and configure Ubuntu 20.04 LTS hosts for Ansible automation. Thanks for reading this article.