Now in the previous video we just mentioned the importance of the Ansible In
ventory,
so it does make sense that we look at it in a little bit more detail and what
we're going to do
is to actually create an Ansible Inventory file, not just this though, we also
have another very
important consideration and this is the creation of our Ansible Configuration
file, this is our
Ansible.cfg file, so we mentioned what the inventory was, what on earth is this
one then? Well as always, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
one then? Well as always,
what I like to do is to briefly go to the documentation, there we can get the
information directly,
so let's just open up a browser and do just that. So if you just search for Ans
ible Configuration
file, let's go down to this one right here, Ansible Configuration Settings. Now
what it says here
is that Ansible is going to support several sources for configuring its
behavior, including an
I and I file known as Ansible.cfg. Now quite simply, there is an order of
operations to actually
follow here. What we're actually seeing here are four different locations WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
follow here. What we're actually seeing here are four different locations
whereby we can specify
this particular file. Now by default, you're going to have this particular file
within the essay
Ansible directory. However, that will be superseded if you happen to define an
Ansible Cfg file in
your home directory, which again will be superseded if you have an Ansible.cfg
in the current directory
that you happen to be working from. And that also can be superseded with an Ans
ible_Config
environment variable. Now if you want to see a sample configuration file that WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
environment variable. Now if you want to see a sample configuration file that
has all of the
available options, then you can run this particular command once you have Ans
ible installed. But in my
case here, it's going to be a fairly simple file, so I'll just skip over and
just generate my own.
So first things first, if I go back to my directory here, all I'm on right here
is an Ubuntu Linux
machine and this is going to act as my Ansible control mode. This is where the
automation is going
to happen and this is where Ansible is going to be installed. So with that said
, let's go and
first install Ansible. Now the way we can actually do this on Ubuntu is if I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
first install Ansible. Now the way we can actually do this on Ubuntu is if I
say pseudo-apt install
Ansible and hit Enter, I'm going to have to type in my administrator password
and enter the game.
And then I'll say why and finally enter once more, give this a moment or two to
complete. And then
eventually you will have Ansible installed on your system, which as we can see
right here, if I say
Ansible-dash version, and to enter, we clearly have Ansible now installed. So
in order to begin
automation, what I'm going to do to keep things nice and neat is to create my
own little folder. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
own little folder.
So to do this on Linux, I will say MKDAR to make it directory, i.e. make a
folder. And now I'll just
specify the name of the folder that I want to create. Let's just call this Ans
ible-automation. The name
itself doesn't ask the matter, you can choose anything you wish. And if I say
CD Ansible-automation,
this will allow me to change directories and go inside of that folder, okay? So
this is where I'm going
to actually be working from within this particular folder. Now if I list the
contents of this directory
by saying LS, we can see there is nothing here. So what I'm first going to do WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
by saying LS, we can see there is nothing here. So what I'm first going to do
is I'm going to create a
file and I can do this by saying touch and then the name of the file. Now
remember, the file is going to be
called Ansible.CFG. This is actually convention we want to use. It's what Ans
ible is going to look for.
And if I do a listing now, we can see we now have this particular file in this
directory structure,
albeit it is an empty file at the moment. Now what I'm also going to do is
within the same directory,
I'm going to create this inventory files. If I just say touch inventory dot i
and i because this
is going to be in the i and i format, albeit like I say, if you so wish, you WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is going to be in the i and i format, albeit like I say, if you so wish, you
can create a YAML file,
but for simple directories or rather for simple topologies, I just prefer to
use i and i. So this is
what we'll use. So real list the contents of this directory. Now we see we have
two particular files.
Let's first go into the Ansible.CFG file. So let me just open this file up
right here. Now within
square brackets at the top, I'm going to have this directive called defaults.
This is going to
allow me to specify some default settings. So with this now done, what I'm
going to say is gathering WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
going to say is gathering
is equal to explicit. And what this is going to do is going to prevent Ansible
from trying to learn
all this information about all my devices every time it happens to run. It's
just unnecessary and
it's going to take up too much time. So this will effectively turn this off
unless I explicitly
tell Ansible to go fat gathering. Next, I'm going to specify the name of my
inventory. So I'll say
the inventory is equal to now what was the name of the file I just created.
Well, it was called
inventory dot ionized. So this is what I'll specify right here. Now what I will WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
inventory dot ionized. So this is what I'll specify right here. Now what I will
now say is host
underscore key underscore checking is equal to false pretty much. We're not
going to worry about ssh key
checking. Basically because it's a lab environment and it's not really
necessary. So the next thing I will
do is after I've specified my default settings, what I now want to do is to
specify how I'm going to
be connecting to my devices. So I'll say persistent underscore connection. And
it will be helpful if I
could not match the keypad. Try that again. So persistent underscore connection
. And inside of here, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
. And inside of here,
I will say ssh underscore type and the configuration value is going to be param
. All param.
Ego is as an ssh Python library that Ansible can use to establish the
connection to my devices over ssh.
So if I just save this right here, this is going to be my Ansible dot CFG file
with this now done.
What I want to do is I want to create my inventory file. Okay. So if I just go
back to my
topology right here, if I go to one of my devices here and just open up a
terminal, what I'm going to
do is to configure my device to accept some basic connectivity over ssh. So I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
do is to configure my device to accept some basic connectivity over ssh. So I
'll go into enable modes.
I'll then say configure terminal to go into global config so I can make changes
. I will then give
the device a host name and let's just call this one s1. I'll then give it an IP
domain name,
just also IP domain name and give it off Cisco dot com. I'll then say the user
name to login will be
admin. The privilege will be 15 and the password will be let's go with admin
pass. Okay. Now I'll turn on ssh by
saying I PSSH version two and then I'll generate my cryptographic keys. So I'll WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
saying I PSSH version two and then I'll generate my cryptographic keys. So I'll
say cryptok he generates RSA
with a modulus of one, oh, two, four. And now I'll say line VTY zero to four.
And then I'll say transport
input ssh and then I'll say log in local to use the local credentials with this
now done. I'll then go
on to the interface for that device. So in my case here, I'm going to be
connecting over eth zero zero,
which is this one right here. So in eth zero zero, I will say no switch port, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
which is this one right here. So in eth zero zero, I will say no switch port,
no shutdown. And now
give it an IP address, I'll say IP address and I'll give it an address within
my local network. So
I'll do one, nine to one six eight one dot one, one and give it a slash 24 mask
. So what's this now done?
What I should be able to do is from my Ubuntu machine, which if I go back here,
we can see this is
over here, because this is on my local network, I should be able to log in via
ssh to this particular
device. So first things first, let's try to ping the device by saying ping one
nine to one six eight
one dot one on one, which is IP address. We're getting a response. Let's press WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
one dot one on one, which is IP address. We're getting a response. Let's press
control C to stop this.
And now also ssh, tap the username of admin and then give that IP address again
. So one nine to one six
eight one dot one, one. So if I hit enter, what I will now do is say yes to
accept the fingerprint.
And now if I tap in the password of admin pass, as we can see, I can now
connect to my device over ssh,
which means I can now use Ansible on this Linux machine to manage that device.
So all I will do very,
very briefly is able to do the same configurations for the remaining three WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
very briefly is able to do the same configurations for the remaining three
devices s two s three and
s four, giving this the IP address of one or two. This will be one or three and
this will be one
or four. So in the interest of time, let me just pause this video and do just
that. So with this
now configured, I should be able to log in to device number two, which I can
just type in the password.
There we go. Arle up device three, accept the fingerprint, type in admin pass
password, and
lastly device number four, accept the fingerprint. Admin pass and to enter, as WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
lastly device number four, accept the fingerprint. Admin pass and to enter, as
we can see, we're
able to log in. So with this information here, we want to pass it all into this
particular file,
the inventory dot I and I file. So let's open this one up now. Now if you want
to see more examples,
what you can do is Google for Ansible inventory, click this link right here.
And what we're going to
see here is some examples of being able to create and build out this inventory.
This example is
showing an Ansible inventory in the I and I format. Ultimately, these square
brackets are allowing us
to specify groups and below. These are the members of the group. So in this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to specify groups and below. These are the members of the group. So in this
case here, we're using
the DNS names as opposed to IP addresses. But if you so wish, you can't use the
IP addresses
directly. Similarly, this is the same type of thing. This time though, this is
just specifying the
inventory in YAML format. So there's a great emphasis on indentation, as we can
see right here.
Now, first thing to note about Ansible is that the inventory is automatically
going to create
two default groups. This is the all group and the ungrouped group. Okay. Bit of
a mouthful. But
basically, the all group is going to automatically infer every single host that WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
basically, the all group is going to automatically infer every single host that
you have in your inventory
file. So pretty much if you tell Ansible within a playbook to target the all
group that Ansible knows
hey, every single file or rather every single device in that inventory file, I
'm going to connect to
it and do whatever the task that the playbook specifies. Similarly, this un
grouped group,
this is going to contain all the hosts that don't have another group aside from
its default membership
to the all group. Okay. So as we can see here, by default, every single host WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to the all group. Okay. So as we can see here, by default, every single host
will always belong to
at least two groups. Now, if you happen to read the documentation, you can
learn that Ansible allows
you to have hosts in multiple groups. And there is an order of precedence to
work through as to resolve
particular conflicts. For our case right here, let's keep it simple. Let's just
stick with a basic
inventory and we'll use the most simple format, the I and I format. Okay. So
let's just go and write
out the configs that we just saw there. Okay. So here's the thing. The way we
actually specify a group
is entirely up to you. However, makes sense to you is perfectly fine. Maybe you WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is entirely up to you. However, makes sense to you is perfectly fine. Maybe you
want to create groups
by device type, you know, like every device in my network or let's maybe say
half of my devices are
Cisco devices, the other half are Juniper devices. Maybe you want to specify
that in a group so you
can logically target particular devices. Then you can do just that or maybe you
want to group things
up via location, you know, somewhere in Europe, somewhere in the USA, that type
of thing once again,
entirely up to you. Some people lead to group things by function. So maybe you
've got particular
devices and the core of the network. Maybe you've got devices and the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
devices and the core of the network. Maybe you've got devices and the
distribution portion of the
network. And then maybe you've got devices and the access portion of the
network. Again, there is
no right or wrong answer. This design is up to you. In my case here, what I'm
going to do is I'm going
to have a group called Cisco, which is basically going to target every device
that I have so far because
right now in my network, I only have four Cisco devices. But what I will also
do is I'll also subdivide
this into a USA group as well as well as a UK group. So basically I've got four WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this into a USA group as well as well as a UK group. So basically I've got four
devices. They're all
Cisco devices to them in the UK and the other two are in the USA. So let's go
and write this out then.
So at the very top, I'll have a group and I'll just say in brackets Cisco. Now
what I will do here is
I'll just give a shorthand name. I will say S1, S2, S3 and S4. Now because this
is the first time I am
referencing these particular keywords S1, S2, S3, S4, I also want to associate
them with their IP address
because I don't actually have DNS resolution at play right now. So what I will WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
because I don't actually have DNS resolution at play right now. So what I will
say here is
Ansible_host is equal to and then I'll type in IP address of the first device
which is 192, 168,
1.101 and then just do the same for the remaining ones. Okay, so S2 is going to
correspond to like I say
192, 168, 1.102 address and S3 will get 103 and S4 you guessed it's going to be
104. So right now what
I've got here is my Cisco group and let's now create my USA group. So if I just WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I've got here is my Cisco group and let's now create my USA group. So if I just
go down,
have my square brackets, I'll just see USA and now this time it's going to have
devices S2 and S1.
So all I will just do is say S1 and S2 and I no longer have to associate the IP
addresses because
it's already been inferred earlier on within the file. So what I can now do
here is create my UK group
and I can just say in the UK we've got S3 and we now have S4. So right now with
an inventory all we've
got is the connection details for the IP address and associating that with a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
got is the connection details for the IP address and associating that with a
particular name. What I want
to do is I want to pass in some particular values associated with this group
membership within the
inventory that is going to allow Ansible to find out what the login username is
, the login password,
as well as the actual device type that we're connecting to. So let's do that in
the very next video.