Ansible is a great tool that can easily manage many servers. With Ansible, you can automate your server configurations and frequently used tasks with one click.
After completing the Ansible installation, you can attach the nodes to the main server and then start preparing Ansible Playbooks. You can follow this link to learn more information about Ansible.
- At least 2 computers or virtual environments. (One for the server and one for the node)
- Ubuntu Server 18.04 (This guide will use this operating system)
- User accounts with SUDO privileges
Update & Upgrade
First of all, you need to update and upgrade your servers. Note that, after this process, the server may require to restart. Make sure that you have complete this process on both machines. To update and upgrade your server, execute the following commands:
sudo apt-get update
We have updated the package information from all configured sources. Now, its time to upgrade if any package available.
sudo apt-get upgrade
software-properties-common package, so ensure that the package is installed.
sudo apt install software-properties-common
Finally, we need to install Python. Ansible uses Python to perform operations.
sudo apt install python
Ansible Installation on Main Server
After updating and installing required packages, we are ready install Ansible. Here are the steps to install Ansible:
- Connect to the main server that will host the Ansible.
- Install the required repository on the system.
- Update the source list.
- Install the Ansible
- Configure SSH keys.
- Configure the connection between the Ansible server and the Node.
Installing Required Repository
With the following command, we are going to add repository to our source list.
sudo apt-add-repository --yes --update ppa:ansible/ansible
Then, we should update the source list again.
sudo apt-get update
Now, its time start Ansible installation. Ansible can be installed by running the command below.
sudo apt install ansible
We have installed Ansible successfully. Installation can be checked by running
ansible command on the console.
Configure SSH Keys
After we have installed the Ansible, we might continue to establish connection between Main server and the Node.
On the main server, we will create the SSH key. On the
Enter file in which to save the key part, you can leave it empty as default. The other questions will be passed empty in this guide.
When you generate the SSH key, it will be stored in the
/home/<user>/.ssh/id_rsa.pub path by default.
Next, we will display and copy the content of key file.
On the Node, you can follow the steps below:
- Connect to the Node machine.
- Execute the following command:
sudo nano ~/.ssh/authorized_keys
- At the bottom of the file, paste the content copied from the id_rsa.pub file.
- Save and close the file.
These steps will complete the SSH configurations between Main server and Node. There is also a simple way to achieve this process.
This process can be simplified by running following command.
ssh-copy-id <IP or Hostname of the Node>
Testing SSH Configurations
Now, we can test the SSH connections between the Main server and the Node. Run the following command on the Main server and if it connects to the Node, the configurations are working successfully.
ssh <user>@<IP or Hostname>
Configure Ansible Server to Connect to the Node
Finally, we can now connect our Node to the Ansible Server. For this, we have to specify the Node address on Ansible Server.
The Node addresses are stored in the
/etc/ansible/hosts path. On the first line, the pattern will be Server Group that can include more than one Nodes. So, you can send your commands to this group and execute them on multiple Node. On the second line, the Node’s IP address will be placed in here. There are many examples of how to define Nodes on the hosts file as below.
sudo nano /etc/ansible/hosts
Test the connection
The best way to check connections between Ansible Server and the node is ping the node from Ansible.
ansible -m ping all
If you see the success message on the output, congratulations! You are now ready to use Ansible and create Playbooks.