hey guys welcome to the session by intellipaat.
In this session we'll be learning how to install ansible on top of AWS we will
install ansible on master and configure SSH access to ansible host and
before we move on with this session please subscribe to our channel so that
you don't miss our upcoming videos now let us begin with this session so the
first step that you have to do is you'll have to install ansible on the master
right but you do not have to install ansible on the slave that's because
ansible works in such a way that you know ansible installation is just
required on the master on the sleeves it just requires python to be installed if
python is installed it can directly connect to that particular ansible slave
right so this is the first step first step is install ansible on master second
step is to configure SSH from the master to the slave that is you should be able
to do an SSH to the slaves from the master without any key and without any
password so we'll see how we can configure this third step is that we'll
have to setup ansible host and then we'll have to test the connection
alright so we'll do this step by step guys so let's start with step 1 which is
install ansible on the master now in order to do that you'll have to first
launch two instances on AWS now let me just quickly jump on to my AWS console
so guys this is my AWS console guys and the first thing that I have to do is I
have to launch an instance and the OS that I want is Ubuntu the processor
would be T 2 dot micro I click on next what are the number of instances that I
want to launch I'm going to launch two instances I'll click on next move to the
part where you'll have to select the security group now if you don't have a
security group first create a security group and add a rule for all traffic and
it should be basically from anywhere click on review and launch and this will
be done if you already have a security group which has these kind of properties
were can do is you can click here on select an existing security group and
select the security group that has all the rules according to what software
you're installing all right so once this is done guys click on review and launch
and finally review all the settings area and click on launch okay select the key
pair that you have on your system and click on launch instances and this will
launch two instances on AWS for you next step would be guys to basically go to my
management console and naming my instances so let's call my first
instance is as ansible one and let's say my second instance is ansible two okay
and simple one is going to be my master and ansible two is going to be my slave
let's try connected to answer b1 first so I'll go to my putty tool and this
already has my my PPK file selected which is HTM all right so this is
already selected now I'll click on open so I'll click on yes and I have
successfully connected to my master now awesome so I have connected to my master
the next step would be to connect to my slave so for connecting to my slave
again I'll go back to our management console select the second instance copy
the IP address open a new pootie terminal put the IP address over here
let us change the color so that we can differentiate which is one is my master
and which one is my sleeve and yes now we are basically connecting to my slave
let's enter the user name and you say successfully connected to my ansible
slave as well so my green terminal is my ansible master this is ready now and
this is my answer will slave now what are the next steps that I will do first
I will have to update the master and the slave so on my master I will type in the
command sudo apt-get update similarly on my sleeve as well I will
type in the command sudo apt-get update
all right so my machines are now getting updated once I have abated my master
next step would be to install ansible and for that the commands are these
right so once you've updated the machines you'll have to install a
package called software properties common on my master usually when you are
launching an instance on AWS this package is already the latest version so
as you can see it says it's already the newest version which is awesome
the next command that we have to run is you'll have to add the repository for
ansible in your and master right so just type in this command hit enter and this
will basically add the ansible repository to your master next step is
to update the machine so let's update the machine with this command so it says
sudo apt-get update machine is updated and now finally the command to install
ansible hit enter and ansible will now be installed on this particular machine
so while ansible is being installed on this machine guys what do you have to do
on the host or on the slaves you'll have to update the machine which you have
already done and just installed Python okay so this is my master ansible
is successfully installed on a master on my slave I just have to install Python
nothing else no other ansible software so python is being installed on this
machine now and python is installed successfully
okay so ansible is installed on my master Python is installed on my slave
these both are ready right now let's see what is our next step so let me just go
to my slides so the first step was to install ansible on the master I have
done that next step is to configure SSH access to ansible host now what is this
particular step mean this means if I go to my master right now and if I type in
the command SSH ubuntu add the rate the IP address of the slave I will not be
able to connect to my slave right now with this command let's see let's verify
this so I'll go to my AWS management control and this is the IP address for
my sleeve let's try to copy that over here let me also explain you what is
basically happening in this command so I am doing an SSH on this machine with the
username ubantu okay so that's why Ubantu at the rate this IP address now
if I hit enter if I just type yes this is basically asking should it add this
IP address in the list of known IP addresses I click on I just select yes
and this IP address will now be added I hit enter and now as you can see it
says permission denied so you're not able to do an SSH to this instance with
this particular IP address now we have to solve this this should basically be a
success this should be able to connect to these slave through this particular
command right now why are you getting permission denied that's because the
only way to connect to that instance is using the PPK or the PEM file right we
don't have the PEM file in this instance so we'll have to enable my slave to be
able to accept connections from this particular machine so that it does not
require any key like a PEM file or a PPK file or any kind of password now this is
a necessity when you are trying to install ansible now let's see how we can
do this how you can be enabled keyless access into my slave now for doing that
guys the first thing that you will do is you'll have to go inside the directory
dot SSH once you go inside that let me first show you the structure
right now if I do an LS you can see there are two files one is called
authorized keys and there is called no notes what is no notes when we were
executing this particular command SSH open to either a IP address it asked us
whether this IP address should be added to the list of known hosts right so
inside this known was file if I do a cat unknown host you can see that this is
basically the code or this is basically the encrypted form of this IP address
that is that on the slave and which tells it that okay so this is a notes
that I'm connecting on okay if you don't understand this guys don't worry this is
not a part of you getting to know ansible well I'm just telling you this for your
knowledge alright so this is encoded form of the IP address that you'll
be connecting to all right what else is there in this directory you have
something called as authorized keys authorized keys is a file wherein you
will be entering all the machines which can connect to this particular system
right if I do a cat on this file right now you would be able to see that this
is the SSH or this is the key code that of this particular file what is this
file this is the the PEM file that that got downloaded on my system when I
created a key pair right so when I connect it to this instance I connected
using this particular file so this is there in the authorized keys file which
means that if a machine is connecting to this particular instance and has this
particular file as as an argument in that case that allows that machine to
connect right so if I remove this nobody will be able to connect to this instance
even if they have this particular file okay now what I want to do is on my
slave if I go to this particular directory dot SSH and if I do an LS over
here this file that is the authorized keys I want to add one more key in this
which will basically allow my master to connect to my slave now which key I have
to add I'll tell you now now in order for you to generate a key for your
system all you have to do is type in SSH - keygen this will basically create a
key for this particular machine it is going to be unique for this particular
machine all right so SSH - keygen if I hit enter it will ask you to enter the
file in which to save the key do not enter anything just hit enter repeatedly
right and then once you get this kind of a message that means your file has now
been successfully created and where is it created it is always created inside
dotted SSH directory okay now if I do an LS over here I can see there's a file
called ID underscore RSA dot pub okay now the
which is there inside this particular file I'll have to put it inside the
authorized keys of my slave okay so let's try to do a cat on ID underscore
RS a dot well this is the code ID underscore RSA sorry for my mistake this is
the code which we have to enter on my slave now okay so let's copy this code
and on my slave let me do a nano on authorized keys and now let's just paste
the code over here okay we have pasted the code let's save the file and now if
I try to do an SSH again from my master I should be able to do it successfully
so I will just again go to that particular command this is it right let
me copy this command and let me clear the screen lines so I'll clear the
screen so that it's visible clearly now I'll do an SSH on this particular IP
address and now you can see I didn't get an error I basically got logged into my
slave and how do you know that this is the username at the rate I pee 172 3145
176 this is what you're getting on this terminal and if I see over here I have
the same number over here as well once I ready to 3145 176 so I am connected from
this machine on to this machine through SSH now if you want to exit the SSH all
you have to do is type in exit it says connection to this IP address has been
closed and you can see it has reverted back to the same name that was of the
master which is IP 170 231 4488 earlier it was 172 31 45 176 which was
off my sleep okay so I have done my second step successfully which was
basically to configure s assess access to my ansible slave without any password
and without any key right you just enter SSH ubantu at the rate IP address of my
slave and you'll be able to connect to it successfully now what is my third
step my third step is setting up ansible host and testing the connection so I
have SSH enabled on my master it can easily connect to my
slave now without any password and without any key but how will ansible will
connect will have to configure that now right now in order to add or in order
for ansible to know that it has to connect to a particular machine it has
to have its IP address right now where can you add that IP address that IP
address is basically added in this particular location / etc' slash ansible
/ hosts alright hit enter and this is the file that will come up right so
these are all the commented lines are all the templates that you can use to
basically add hosts but I'm going to tell you a new template in any case so
the first the way you add hosts is you start by square braces okay and inside
square braces you give a name let's say the name is production what does this
name symbolize this name means that whatever is below these square braces is
basically inside the group production so this is the group name okay and now what
I can do is I can add n number of IP addresses over here and although the IP
address is basically will be inside the production group okay you can name your
group anything guys it it's basically random you can name it anything but
whatever IP addresses you will specify after that we'll get inside that
particular group okay now I have created a group now I want to
add a host inside this group so I can give my host a name as well or I can
give my slave a name as well so let's call my slave as slave 1 right then it
will give a space and then you will type in ansible underscore ssh underscore
host is equal to and then the IP address of your slave so in my case the IP
address of my slave is this particular IP address let's try to copy it and
let's enter it over here okay so you enter the group name then you enter the
host name of the slave name paste ansible ssh underscore host equals to the IP
address of my slave so whenever I ask ansible to ping any of my slaves now it
will read this particular file to get know which IP address it has to ping on
ok now let's save this file so my file is now saved and now let's try to enter
the command ansible - M so now what I what we are trying to do is we are
trying to see whether ansible can communicate with my slave ok now in
order to do that I will type in ansible - M ping ping means ping that particular
all the slaves which are basically connected to this ansible master how how
we selecting all the slaves will type in the command all so ansible - M ping ping
on what all the machines that are connected to the sensible master I hit
enter so it will go through that file which we created slash HTC slash and
simple slash hosts it will see all the hosts which are added in that particular
file and then it'll ping those machines right so as you can see it on slave one
slave one was the name that I gave to my host on slave 1 it was successfully able
to send ping and what it receive was pong which means that it was
successfully be able to communicate with the ansible slave alright I can also
type in ansible - M ping and I can specify the group name so the group name
was production let's say right I hit enter as you can see it automatically
got to know ok so if I'm pinging production these are the hosts which are
inside it and this is the message that you are getting which is success if I
type try to type in the host name as well so I can also do that I'll type in
ansible - M ping slave 1 which is the host name hit enter it will again be
able to ping the slave machine but let's say I enter a name which is not there in
the host file what will happen in that case
it will not be able to match it to any of the name presently the host file and
it hence you will get an error no host match nothing to do ok so guys this is
how you can setup ansible master slave on AWS using using the ansible software
which basically does not require you to install the ansible software on the
slave just install ansible master on the master sorry the ansible software on
the master installed Python on the slave add the IP address of the slave in the
hosts file and configure keyless SSH access from the master on the slave and
you're ready to go all right so guys with this we conclude our session so
let me just come back to my slide so let us summarize what we did we installed
ansible on the master and we installed Python on the slave then we configured
keyless SSH access from the master to the slave and the third step was we
added the hosts IP address that on my ansible master in the file / etc'
slash ansible slash hosts right once I did that I was able to communicate with
my slave okay guys we've come to the end of this session
I hope this session was informative and helpful if you have any queries
regarding this session please feel free to come into it below and we love to
help you out thank you