Ansible is a server management tool, and you can install it on Ubuntu to manage servers from a single location. The advantage over other server management tools is that it allows configuration using SSH and YAML files. In addition, it doesn’t require any special software.
In this article, you will learn how to install Ansible on Ubuntu. You’ll also cover the Ansible control, host node configuration, and SSH Key Pair setup for the Ansible connections.
For this tutorial, you will need one server and two or more client machines running Ubuntu. In addition, there should be an SSH configuration on all the nodes. Furthermore, you should have the SSH Public Key of the Server saved in the authentication_keys file.
Nodes in Ansible
- Control Node: this node is responsible for managing all the host nodes. It can be a physical machine or a remote server.
- Host node: the machines that are controlled by the Ansible Control Node.
Install Ansible On Ubuntu
This tutorial covers the Ansible setup from scratch. That means you will also learn how to set up SSH and SSH servers on the Control and Host nodes. So, let’s get started with the installation.
1. Configure Control Node
First, log in to the Ubuntu machine as a root user. Next, add the newly created user as an administrator. For this step, execute the command given below:
Make sure to replace the username with the new user that you just created. For example:
Answer all the questions that appear on the screen. Remember to add a strong password for the new user. Alternatively, press the “Enter” key if you want to skip the information part. The output should look something like this:
Secondly, assign administrative rights to the new user. For this step, use the
usermod -aG sudo [username]
usermod -aG sudo mary
2. Configure SSH Key Pair for Control Node
Log out from the current root user, and log in to the new account. Open the terminal using the shortcut key “Ctrl + Alt + T”. In this step, you will generate the SSH Key Pair for this account. Use the
ssh-keygen command for this step. For instance:
If the SSH Key Pair already exists in the system, it will show you the following output:
You can choose to overwrite the existing pair or leave it as is.
Alternatively, if there exists no SSH Key Pair, you will get the following output:
Enter a strong passphrase to generate SSH Key.
3. Configure Host Node
This step covers configuring the nodes that the Ansible Control Node will control. Each of these nodes uses Control Node’s SSH Key. To get started with this configuration, first install the SSH server. For example:
sudo apt install openssh-server -y
The output should look something like this:
Confirm that the service is running as soon as the OpenSSH is installed. Use the
systemctl command for this step. For instance:
sudo systemctl status sshd
You should be able to see the service running:
Alternatively, if the service is not running, start it manually. Specifically, type:
sudo systemctl start sshd
Also, make sure to enable the ssh service on boot.
sudo systemctl enable sshd
Next, configure the ufw firewall to allow SSH connections. For instance, type:
sudo ufw allow ssh
A “Rule Updated” message on the console ensures that ssh is allowed:
Now, look up the IP address of the Host Node with the hostname command. Type:
Here, the IP address is 192.168.19.163. Yours will be different, so make sure to use that one for your connections.
Next, copy the SSH Key as shown below:
ssh-copy-id [email protected]_host
Replace the [email protected]_host with your host node. For example:
ssh-copy-id [email protected]
The output should look something like this:
Type “yes” and press the “Enter” key to continue. This command will now search for the public key you generated in the previous step. Once it finds the public key, it will ask for the remote host password. Enter the remote host password, and press the “Enter” key. This step will upload the public key of the Control Node to the Host Node’s account.
4. Install Ansible on Ubuntu
Finally, you are now ready to install Ansible on the Control Node after all the configurations. First, make sure that all the packages and repositories are up-to-date. Specifically, type:
sudo apt update && sudo apt upgrade
You should get a similar output as this:
Secondly, use the
install command to install Ansible. For example:
sudo apt install ansible
Press “Y” to continue the installation and wait for the process to complete as shown below:
5. Set Up Ansible Inventory File on Ubuntu
Next, you will set up the inventory file. This file consists of information on all the nodes that Ansible will control. To access this file, use the nano or vim editor. For example:
sudo nano /etc/ansible/hosts
After the file opens, you will add host nodes. Scroll down to the bottom of the file and add information in the following syntax:
[group name] server_name ansible_host=[server_ip]
Server server_name ansible_host=127.0.1.1
Once you are done, save the file and exit the editor. In addition, to view the inventory file settings, use the
–list option. For example:
ansible-inventory --list -y
6. Test the Connection
The final step in the Ansible configuration is to test the connection between control and host nodes. Open the terminal in the control node. Ensure that you have logged in from the new non-root use you created in Step 1. Type:
ansible all -m ping
Alternatively, if you have logged in from the root user, don’t forget to add the
-u flag. For example:
ansible all -m ping -u root
Since you are testing the connection for the first time, you may get the host authentication prompt. Type “yes” and hit the “Enter” key to confirm. If you get a similar output, your hosts are up and running.
And that’s a wrap! In conclusion, this guide covered the basic node setup and SSH configuration for Ansible. You have also learned how to install Ansible on Ubuntu. So now you are ready to configure Ansible commands and execute playbooks on the remote nodes.
For more information on Ansible and its playbook, check out Ansible documentation. We hope the article was useful to you. In case you have any queries, drop them in the comments section.
If this guide helped you, please share it.