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

Hey guy’s 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.

Image for post
Image for post
Article review

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 most demanding Configuration management tool and application-deployment tool through Infrastructure as a Code(IaaC).

Now you might be saying What the hell is 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 beginner or totally new to Linux just consider configuring server is just a three step process if you are not doing any additional configuration or stuff.

Now you can do these three step in your device very easily but if I ask you to do 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 you 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.

Now let’s consider you want to deploy a web application And to deploy web app you need to configure a server, then you need database to store user data and website related data, configure load balancer, Scale app 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 it 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 a agentless tool means you just need to install Ansible on one machine and using that machine you can configure others very easily.

Another benefit of Ansible is it use YAML syntax which is almost similar to JSON even more simple to learn while with chef you need know basic syntax of ruby.

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

As for beginner perspective configuring Ansible could be little tedious or you might not have enough system configuration so that you can perform practical So I am going to show you all three way of installation 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 in 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 command I will show you will be RedHat Linux based.

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

There are 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 come with both version of 2 and 3 of python. To check if the python is installed in your system or not use command

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

You might see output something like this.

Image for post
Image for post
install python output

If you don’t see output as shown in above image mean you don’t have python installed in your system . To install python use 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 took few minutes to half an hour depending upon your internet speed. Now to check you have installed ansible successfully or not use command

ansible --version
Image for post
Image for post
Check ansible version

If you see output as shown above it mean 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 other 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 Inventory file we write the SSH information about the managed nodes. Because Ansible Behind the scene use SSH protocol to SSH we require three information.

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

You can create inventory file with whatever filename you like wherever in system you want. To make things little simple I am creating inventory file with name “host.txt” in /ect/. In 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 inventory file you can write SSH information of N number of managed node

Now we have created 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:-

Image for post
Image for post
ping output

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

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

Let’s Install Ansible on Cloud:-

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

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

If you don’t know how lunch ec2 instance in AWS you can fellow the below step to lunch it but if you are familiar with ec2 instance and know how to lunch one you can skip those step 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 in this practical I am using RedHat AMI
  4. Choose Instance type you can choose any but to make this practical free of cost I am using t2.micro and click on Configure Instance Details.
  5. Give number of instance you want to lunch here I am want to lunch 2 instance one of them act as managed node and other one act as controller node
  6. Select network and Subnet. Remember network must be same as VCP we selected in security group. 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 appear here 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 login. → finally click on Lunch instance.

Now you have 2 instance or you can say OS running in cloud you have to login for which either you can use software called putty if you are windows user or use SSH if you are linux as well as window user.

Here I am using SSH to login to Ec2 instance. For this the command is

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

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

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

If your instance don’t have python installed already use command.

yum install python36 -y 
or
dnf install python36 -y

But to run 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 instance we are going to configure that instance to be a controller node. For this create inventory file with whatever filename you like wherever in system you want. To make things little simple I am creating inventory file with name “host.txt” in /ect/. In Inventory file you have to write

<Public IP address of managed node or instance>

For example:-

54.147.249.151

In inventory file you write public IP address of managed node

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

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

Now we have already created inventory file and moved 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

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:-

Image for post
Image for post
pint output

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

Let’s Install Ansible on docker container:-

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

Now to install ansible inside container you must have python installed inside of it but more importantly you must enable docker container to use SSH protocol. However in this practical I am not going to show you how you can enable SSH protocol inside docker container or create 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 image 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 inventory file in controller node container with whatever filename you like wherever in container you want. To make things little simple I am creating inventory file with name “host.txt” in /ect/. In Inventory file you have to write 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 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:-

Image for post
Image for post
ping output

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

Common troubleshooting technique:-

  1. If you see output something like shown below
Image for post
Image for post
common error of finger print

Means you did SSH before to the system now 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

Image for post
Image for post
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 own 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. Engineer means Manipulator. Life & stuff are already complicated but I believe in simplicity and I try to manipulate things 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