Ansible

How to install Ansible on Ubuntu?

3 Mins read

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.

automation environment

Ansible Installation


Requirements

  • 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
  • Python

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
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
apt get upgrade

Ansible requires software-properties-common package, so ensure that the package is installed.

sudo apt install software-properties-common
software properties common installation

Finally, we need to install Python. Ansible uses Python to perform operations.

sudo apt install python
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:

  1. Connect to the main server that will host the Ansible.
  2. Install the required repository on the system.
  3. Update the source list.
  4. Install the Ansible
  5. Configure SSH keys.
  6. 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
add ansible apt repository

Then, we should update the source list again.

sudo apt-get update
apt get update ansible repo

Install Ansible

Now, its time start Ansible installation. Ansible can be installed by running the command below.

sudo apt install ansible
install ansible

We have installed Ansible successfully. Installation can be checked by running ansible command on the console.

check ansible installed

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.

ssh-keygen
ssh keygen

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.

cat ~/.ssh/id_rsa.pub
cat ssh key

On the Node, you can follow the steps below:

  1. Connect to the Node machine.
  2. Execute the following command: sudo nano ~/.ssh/authorized_keys
  3. At the bottom of the file, paste the content copied from the id_rsa.pub file.
  4. 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>

ssh-copy-id 192.168.31.102
ssh copy id
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>

ssh strolve@192.168.31.102
ssh test

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
ansible host edit
[webservers]
192.168.31.102

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
ansible ping test

If you see the success message on the output, congratulations! You are now ready to use Ansible and create Playbooks.

Recep Duman
26 posts

About author
I am a fan of programming, innovation, and web development. I’m also interested in technology and entrepreneurship.
Articles
Related posts
Ansible

What is Ansible? How does Ansible work?

2 Mins read
Ansible is an open-source platform developed with Python and Ruby programming languages that allow you to manage or configure servers or network…
Subscribe to Newsletter

To get the latest news, subscribe our channel.

Leave a Reply

Your email address will not be published. Required fields are marked *

×
Ansible

What is Ansible? How does Ansible work?