Ubuntu

How to Install and Configure Ansible on Ubuntu 22.04

Ansible is an open-source automation tool used for IoT tasks including software deployment, configuration management, cloud provisioning, infra services orchestration and more. It runs on many Linux operating systems and is configured quite easily. It uses the open SSH service to perform the various networking tasks ensuring a well secure and protected communication environment.

How to install Ansible on Ubuntu 22.04

In this tutorial, we will guide you how you can install Ansible on Ubuntu 22.04 using the easiest method whose steps are given below and, in the end, we will also show you how you can configure Ansible to use it for automation.

Steps to install Ansible on Ubuntu 22.04

Step 1: First, install some useful required packages if not already installed for running Ansible on Ubuntu 22.04 using the following command:

$ sudo apt install software-properties-common

Step 2: Now, add the Ansible repository in order to successfully install the latest Ansible version on Ubuntu 22.04:

$ sudo add-apt-repository --yes --update ppa:ansible/ansible

Step 3: Once the repository is added, you can then execute the Ansible installation through the following command:

$ sudo apt install ansible

After completing the Ansible installation, use the following command to check its version:

$ ansible --version

Steps to Configure Ansible on Ubuntu 22.04

Now, the next task is to configure Ubuntu hosts for Ansible automation. You can automate Ansible using multiple hosts and setting up each host requires exactly the same step performed for the initial host.

To configure a host, you will need to perform the following steps.

Step 1: First, install OpenSSH via the following command:

$ sudo apt install openssh-server

You can also check the status of SSH using the following command:

$ sudo systemctl status sshd

Step 2: Next, configure firewall to allow the SSH access on Ubuntu 22.04 using the following command:

$ sudo ufw allow ssh

Step 3: Now, create an Ansible user through the below-given command. This step will require setting up a password of your own:

$ sudo adduser ansible

After updating the password, you will be required to provide your full name and other information:

Enter “Y” to complete the step.

This will add the Ansible user.

Step 4: Now, execute the below-given command to allow Ansible users to enable password-less sudo access:

$ echo "ansible ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/ansible

Step 5: Next, Generate SSH keys using the below mentioned command:

$ ssh-keygen

Press Enter in all next options without typing anything to generate SSH keys:

Step 6: Next, copy the SSH public key to Ansible host using the following command.

$ ssh-copy-id ansible@<Ubuntu_hostaddress>

Type “yes” in the next option:

Set password for first user.

This will add the first user as “ansible”. You can add more you want using the above steps.

Step 7: Once the hosts are created, you can then use the following command to add hosts:

$ nano hosts

In our case, we have only one host, you can add other hosts. Save the file after adding the hosts. Then use the following command to ping the hosts.

$ ansible all -i ./hosts -u ansible -m ping

That’s it, the above command’s output ensures that you have perfectly set up Ansible on your Ubuntu.

Conclusion

Ansible is an excellent IoT automation tool that allows users the opportunity to implement different IoT tasks including software deployment, configuration management and more. The installation is pretty simple on Ubuntu while the main thing matters is how you configure Ansible. The above guidance will be helpful in installing and setting up Ansible on Ubuntu 22.04.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.