Let’s configure Ansible on Local machine, Cloud and Container

Hey, guys In this article I am going to show you how you can install and configure Ansible in your local machine, on cloud like AWS, and on container technology like Docker or Podman or cri-o.

Article View

Before I jump to installation part Let me explain you some basic terminologies and concept which will help you in installation if you decided to do practical along with me.

What is Ansible?

Now if you are new to the Ansible or heard this term somewhere but don’t know what it is? Let me help you there.

Ansible is one of the most demanding configuration management tools and application-deployment tools through Infrastructure as a Code(IaaC).

Now you might be saying What the hell is a Configuration management tool? or What is Infrastructure as code or something like this but don’t worry let me explain them to you in a simple way.

If you ever configure any server like web server, SSH server or mail server, or FTP server you might know it is just a simple three-step process but if you are a beginner or totally new to Linux just consider configuring a server is just a three-step process if you are not doing any additional configuration or stuff.

Now you can do this three-step in your device very easily but if I ask you to do the same thing in 10 devices I know you can do that too but what about 100 devices or 1000 devices not very easy. Is it? and there is the role of configuration management tool come into play, You can configure all of the 100 or 1000 devices without going to these devices physically. You only have to run one file and Voila your work is done. Configuration management tools can manage or can do all configuration-related stuff.

Now let’s consider you want to deploy a web application And to deploy a web app you need to configure a server, then you need a database to store user data and website-related data, configure the load balancer, Scale as per requirement, configure firewall, and security stuff which is nothing but configuring infrastructure you needed to deploy your app. We can automate all of this by writing a code instead of performing all the commands manually.

Why Ansible?

If you ever heard about some other configuration management tool like Puppet, Chef, SaltStack you might be asking that question. So let me answer that too.

The major benefit with ansible is Ansible is an agentless tool means you just need to install Ansible on one machine and using that machine you can configure other machines very easily.

Another benefit of Ansible is it uses YAML syntax which is almost similar to JSON even more simple to learn while with the chef you need to know the basic syntax of ruby. If you don’t know what YAML or JSON is? They are declarative language mostly prefer to make communication between different tools or systems.

Okay! Enough theory and you might be getting bored with all these theories So I am going to jump to the Installation part.

As for beginner perspective configuring Ansible could be a little tedious or you might not have enough system configuration to perform ansible practical So I am going to show you all three-way of installing and configuring Ansible. You can go for any as per your requirement.

Let’s Install Ansible on Local Machine.

Now if you want to follow me along you should have Linux installed in your system either as Base Operating System or a Virtual Machine. If you have very low system configuration or want to know how you can install Linux OS in CLI, I do recommend you please check out my article on Let’s Install Linux In CLI where I showed you How you can install Linux In CLI.

NOTE:- Most of the commands I will show you will be RedHat Linux-based.

Now I am considering you have installed Linux so Let’s get started.

There is two way by using which we can use to install Ansible.

  1. By using YUM or DNF or your Linux flavor respective package manager.
  2. By using PIP.

To make the installation of Ansible more generalize I am going to use PIP to show you the installation of Ansible. For PIP you must have python installed in your system. When you install Linux it already comes with version 3 of python. To check if the python is installed in your system or not use the command:-

rpm -q python36
or
rpm -q -f /usr/bin/python3

You might see output something like this:-

install python output

If you don’t see the output as shown in the above image means you don’t have python installed in your system. To install python use the command:-

yum install python36 -y 
or
dnf install python36 -y

Now you have python installed in your system. To install ansible run command:-

pip3 install ansible

Ansible installation might take few minutes to half an hour depending upon your internet speed. Now to check you have installed ansible successfully or not use the command:-

ansible --version
Check ansible version

If you see the output as shown above it means you are have installed Ansible successfully. Now before I jump to the configuration part of ansible you really need to understand two terminologies.

  1. Control Node:- The System or host on which we installed Ansible and use that system to configure another system we called that system Control Node.
  2. Manage Node:- The system which we configure by using ansible we call that system Manage Nodes.

To configure ansible we have to first create a text file which we called inventory file in ansible. In the Inventory file, we write the SSH information about the managed nodes. Because Ansible Behind the scene uses SSH protocol to SSH we require three pieces of information.

  1. IP address of machine you want to SSH
  2. username
  3. and password of that user.

You can create an inventory file with whatever filename you like wherever in the system you want. To make things a little simple I am creating an inventory file with the name “host.txt” in /ect/. In the Inventory file, you have to write:-

<IP address of managed node >ansible_ssh_user=<username> ansible_ssh_pass=<password>

For example:-

192.168.1.7  ansible_ssh_user=root ansible_ssh_pass=123456

In the inventory file, you can write SSH information of N number of a managed node

Now we have created an inventory file let configure ansible.

To configure ansible Follow the step mention below:-

  1. Create directory or folder called ansible in /etc/
  2. In directory /etc/ansible/ create a file ansible.cfg
  3. In /etc/ansible/ansible.cfg you have to write
[defaults]
inventory=<path of inventory file>
host_key_checking = False

For example:-

[defaults]
inventory=/etc/host.txt
host_key_checking = False

And that’s it. You configured ansible Now to check you configure ansible properly or note use command:-

ansible all -m ping

You might see the output something like this:-

ping output

If you see the output shown above means you configured ansible properly.

You might see some error messages So at the end of this article I will share some troubleshooting techniques you can use to troubleshoot.

Let’s Install Ansible on Cloud.

Now let’s move to the installation of ansible on the cloud. There are many public cloud providers like AWS, GCP, Azure, DigitalOcean, Oracle, etc. and you can use any of them but to show you this practical I am going to use AWS cloud. If you are new to the cloud world I do recommend you please check out my article on Let’s understand Cloud Computing and Some Case Studies where I explained everything you need to know as a beginner about the cloud.

To Install Ansible we need OS and to lunch OS in AWS I am going to use a service called ec2 which AWS provides.

If you don’t know how to lunch an ec2 instance in AWS you can follow the below step to lunch it but if you are familiar with the ec2 instance and know how to lunch one you can skip these steps and directly jump to the installation part.

  1. Go to service and under Compute you will find EC2 → click on it.
  2. Go Instance and Click on Lunch Instance.
  3. Select AMI whatever you like but in this practical, I am using RedHat AMI.
  4. Choose Instance type now you can choose any but to keep this practical free of cost I am using t2.micro and click on Configure Instance Details.
  5. Give the number of instances you want to lunch. Here I am want to lunch 2 instances one of them act as a managed node and the other one act as a controller node.
  6. Select Network and Subnet. Give network and subnet whatever you like. Click on Add storage.
  7. Give some storage and I am giving storage of size 20Ggb. Click on Add tags → Give whatever the tag name and value you like → click on Configure Security Group.
  8. Select Create a new security group and select SSH protocol type →click on Review and Lunch → at last click on lunch.
  9. A window appears where you have to choose “Create new key pair” give a key name and don’t forget to download key pair it. else you won’t be able to log in. → finally, click on Lunch instance.

Now you have 2 instances or you can say OS running in the cloud you have to log in for which either you can use software called putty if you are a windows user or use SSH if you are Linux as well as window user.

Here I am using SSH to log in to the ec2 instance. For this use command:-

ssh -l ec2-user <public ip of instance> -i <key name>.pem
ssh

Now you are logged in inside the instance I am again going to use PIP to install ansible for which you must have python in your system. To check whether you have python installed in your system or not use the command:-

rpm -q python36
or
rpm -q -f /usr/bin/python3

If your instance doesn’t have python installed already use the command:-

yum install python36 -y 
or
dnf install python36 -y

But to run the above command you need root or I say super user privilege to become one you have to use command:-

sudo su - root

To install ansible use command:-

pip3 install ansible

Now we have installed ansible inside the instance we are going to configure that instance to be a controller node. For this create an inventory file with whatever filename you like wherever in the system you want. To make things a little simple I am creating an inventory file with the name “host.txt” in /ect/. In the Inventory file, you have to write.

<Public IP address of managed node or instance>

For example:-

54.147.249.151

In the inventory file, you write the public IP address of the managed node.

To be able to login to the managed node you need to move the private key file which you downloaded during instance creation from the local machine to the controller node (remote machine). If you are using Linux OS then use the SCP command but if you are using a windows machine use WinSCP which you can easily download from here.

Here I am not going to show you how can move a file from a local machine to Linux remote machine. But you can easily found it over here.

Now we have already created the inventory file and moved the private key file to the controller node let’s configure ansible.

To configure ansible Follow the step mention below:-

  1. Create directory or folder called ansible in /etc/
  2. In directory /etc/ansible/ create a file ansible.cfg
  3. In /etc/ansible/ansible.cfg you have to write
[defaults]
inventory=<path of inventory file>
host_key_checking = False
private_key_file = /home/ec2-user/<filename>.pem
remote_user = ec2-user
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = False

For Example:-

[defaults]
inventory = /etc/host.txt
host_key_checking = False
private_key_file = /home/ec2-user/ansible.pem
remote_user = ec2-user
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = False

Note:- In the above configuration file I have written some keywords which you might not able to understand if you are a beginner. In single words, you can consider I have written sudo command. of Linux. I am not explaining these keywords to keep this article little light.

And voila. You configured ansible in the controller node. Now to check you configure ansible properly or not use the command:-

ansible all -m ping

You might see the output something like this:-

pint output

If you see the output shown above means you configured ansible properly in your controller node.

Let’s Install Ansible on docker container:-

For this practical, you must have some knowledge of docker or podman or any of the container technology.

Now to install ansible inside the container you must have python installed inside of it but more importantly, you must enable the docker container to use SSH protocol. However, in this practical, I am not going to show you how you can enable SSH protocol inside a docker container or create an SSH enable docker image. But soon I am going to write an article where I am going to show you how you can enable SSH inside docker.

I have already have created two docker images one with preinstalled ansible I called ansibleos which you can find here and another one is SSH enabled docker image which I called sshos which you find here. Download/pull both the image and lunch one container of each of them.

For this use command:-

docker container run -it --name <container name > <image-name>

Now for this practical, I am considering the name of the container you created using ansibleos docker image is controller node. and name of the container you created using sshos docker image is managed node.

For example:-

docker container run -it --name controller node ansilbeosdocker container run -it --name managed node sshos

Now just create an inventory file in the controller node container with whatever filename you like wherever in the container you want. To make things a little simple I am creating an inventory file with the name “host.txt” in /ect/. In the Inventory file, you have to write the IP of managed node container.

<IP address of managed node >ansible_ssh_user=<username> ansible_ssh_pass=<password>

For example:-

172.17.0.3 ansible_ssh_user=root ansible_ssh_pass=123456

Now we have created an inventory file let configure ansible.

To configure ansible follow the step mention below:-

  1. Create directory or folder called ansible in /etc/
  2. In directory /etc/ansible/ create a file ansible.cfg
  3. In /etc/ansible/ansible.cfg you have to write
[defaults]
inventory=<path of inventory file>
host_key_checking = False

For example:-

[defaults]
inventory=/etc/host.txt
host_key_checking = False

And that’s it. You configured ansible in container Now to check you configure ansible properly or note use command:-

ansible all -m ping

You might see the output something like this:-

ping output

If you see the output shown above means you configured ansible properly.

Common troubleshooting technique:-

  1. If you see output something like shown below:-
common error of finger print

Means you did SSH before to the system you are using as managed node and the finger print saved in /root/.ssh/known_hosts which we have to remove or delete.

2. If you see output something like shown below:-

common error of sshpass

Means of don’t have an tool called sshpass which basically automate the authentication which we are passing in inventory file. For this use

yum install sshpass -y 
or
dnf install sshpass -y

You might come across some more error or exception which you may not able to troubleshoot on your own or might have some more Queries, Suggestion’s so Feel Free to Connect to me On Linkedin or comment below.

If you like it then Please Clap & Share ..

Thank you EveryOne For reading .!!

I am Engineer. I believe in simplicity. Life and stuff are already complicated so Why make it more complicated. I try to make things simple as simple as I can.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store