Get started with Ansible on any Cloud (GCP, AWS & Azure): Ansible Installation and Configuration

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows.

In this article, instead of going into the theoretical part, we will directly see how we can install and configure Ansible on any Cloud Service.

In this article, we will create our resources on GCP.

We will divide this article mainly into two sections as follow.

  1. Create GCP resources i.e. Compute Instances (VM)
  2. Ansible Installation and Configuration

Now, let's go ahead and complete our Ansible Implementation.

Create GCP resources

For crating resources you need access to GCP, if you don't have subscribed for GCP please go ahead and do that. You can opt for their 3 months free trial where they provide 300$ for practice click here for the GCP subscription page.

Once you have access to GCP Console navigate to Home>Compute Engine>VM Instances

Here, we are going to create 3 machines 1 Master another 2 will be nodes.

Click on Create Instance button, after that a page with multiple attributes will open it includes the stuff related to your VM specification. The page will look like as below.

VM Details Page
VM Details Page

Fill up the details as per your requirements and click on Create button. I have created an ansible-master machine with the following details.

Name: ansible-master

Region and zone: Default

Machine Configuration: General Purpose<Series-E2<Machine Type- e2-medium

Boot Disk: CentOS

Similarly, I’ll create two nodes but here specifications will be less as compared to the master machine.

Finally, I've created the following machines.

Master: ansible-master

Node1: ans-node1

Node2: ans-node2

So here we are done with the Infra creation part now we will move ahead to the second part of our article which is Ansible installation and configuration.

Ansible Installation and Configuration

In Ansible we only install packages on the Master machine and copy those to respective nodes as Ansible is an agentless tool.

Now, go ahead and SSH into your ansible-master, the screen will look like as below

Now enter the following command which will install Ansible on your system.

$ sudo yum install ansible -y

Ansible will be installed in a bit, after installation you can verify the version by executing the following command.

$ ansible --version

In order to perform any task/configuration from the master you need to copy ssh keys into your nodes for that first, we will create the ssh keys into master then we will move those into respective nodes.

Execute the following command in order to generate ssh keys in master.

$ ssh-keygen 

You will see output similar to the data from the below screenshot.

Once you are done with creating SSH keys, copy t.hese keys into respective nodes in our case we have only two nodes.

$ ssh-copy-id root@node1IP$ ssh-copy-id root@node2IP

The above steps make sure that you have access to your respective nodes/servers via ssh.

If you face an issue such as AccessDenied do the following steps in order to get rid of the issue.

First Step: Set root password of all the nodes and master machine, SSH into respective machines, and execute the following command.

$ passwd root

It will ask you to enter the password hit enter after that and again enter the password to confirm the password.

Second Step: We need to change some attributes from sshd_config file to access this file enter the following command.

$ sudo vi /etc/ssh/sshd_config

The screen will look like something as below.

sshd_config file
sshd_config file

You can choose whichever editing tool you want I have used Vim here as I am comfortable with vim.

We need to change attributes of these two fields PubkeyAuthentication and PasswordAuthentication there must be NO after these lines you need to replace NO with YES and if there is # before these attributes remove the #.

Ex. #PubkeyAuthentication no and #PasswordAuthentication no

Replace it with: PubkeyAuthentication yes and PasswordAuthentication yes

If you need to use the root login set PermitRootLogin field to yes

After editing these attributes save the vim file and enter the following command to restart the sshd.

$ systemctl restart sshd

After performing these steps, to check everything is in place please try to ssh to and fro.

Now, you have installed ansible on your machine and ssh settings are also in place, we will add nodes IP in the host's file which is an important step.

Navigate to following the path to do so.

$ vi /etc/ansible/hosts

A file will open, read all the instructions from that file carefully and add the IP addresses of all the nodes in there as per your requirement or at the bottom and save it.

Now to check nodes have been added successfully, we will take the help of the following commands.

$ ansible all --list-hosts

Above command will display all the hosts which you added to the hosts file.

And, finally to check if it is responding or not we will ping the hosts with the help of following command.

$ ansible all -m ping

You'll see an output similar to the following screenshot.

That’s it, guys! you are done with Ansible Installation.

Stay Safe! Happy Learning!