So in the previous video, you wrote a playbook that would ultimately monitor
the memory utilization
or rather the free memory on the devices that we were managing. Now in this
particular video,
I want to do something very, very similar, meaning that we're going to be
monitoring memory once
again, but I want to focus particularly on managing the numerical values so
that we can actually run
tests against particular values. Now what on earth am I even talking about here
? This sounds
super abstract. Well it really isn't. We're going to be talking about this
concept of integers WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
concept of integers
again, all the values of floats and how they happen to be different from what
are known as
string values in programming languages and how this can actually affect us when
we want to make
comparisons. Let me tell you what I mean. So generally within a programming
language,
you'll have a very particular way to specify what is known as a string value.
And commonly,
this might be to use quotation marks. If I said the name john, this would be a
string value. Basically,
it's just like a word. Okay, whereas if I wanted to use in America values, such
as the number four, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
as the number four,
in the case of let's say they say the Python programming language to specify
that this is an
integer and not a string, you would not have your quotation marks. So what is
that that is the
relevance of this? How does this affect things? Well, let's maybe say we want
to run a comparison. Okay,
so let's say you had this year four, but you had it in quotation marks so that
it was denoted as a
string value. If you try to do some type of mathematical operation using the
plus symbol,
and you added in yet another string value, such as five with in quotation marks WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and you added in yet another string value, such as five with in quotation marks
, these would be
interpreted as characters and not actually as a numerical value. So if we add
these two together,
what do you think you would get? You're actually not going to get the value
nine,
you would get the value, the string value that is four and five, which might
look like 45,
very, very different from adding four and five together. Similarly, if you
wanted to have a
variable, let's say you registered the variable, maybe some memory output and
it came out at the
value of let's maybe say 32, if you wanted to run a comparison and say, hey, if
the number is below WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the number is below
50%, so less than 50%, then you would want to effectively shoot off some type
of message,
some type of alerts and hey, the value is lower than expected. Now in order for
this comparison
to operate, these actually have to be acting as integers or floats rather than
string values.
This is what I'm talking about here. So what I just want to do is I want to
briefly take a moment
to perhaps run a command that is going to pull some memory statistics and then
manipulate those values so that we can ensure that the data is coming back as WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
manipulate those values so that we can ensure that the data is coming back as
numerical values
integers so that we can effectively do our tests. So let's go and see how this
actually shakes out
them. So what I will do here is I'll create a new playbook. Let's call this
play two dot y
melgrons and set more to have our three hyphens at the top and we'll get this
play-bicker name,
we'll call it play two and we'll just say testing memory values. So if I target
the host of the
all which is going to be every device in my inventory and I set my gather facts
to be the
billion value of false, I can now define my tasks. Now what I'm going to do WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
billion value of false, I can now define my tasks. Now what I'm going to do
here is I'll give this
first tasking name and I'll just say retrieve free memory. So let me use the
Ansible built-in shell
module. Now this is the command that I want to run here. If I just happened to
let's maybe cat
the proc mem info file right here, we're going to get a whole bunch of memory
statistics. We see
this. Okay, let's scroll on up. So what I want to do here is I want to get the
amount of free memory
which is this value right here. So what I can do is I can just write for that
directly. So let's
move this here and I'll just say grip for mem free and be careful of the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
move this here and I'll just say grip for mem free and be careful of the
capitalization. If I hit
enter right now, look at what we're getting back. So let's go and try to use
this particular
value then. So let's go in here and grab this particular command. Okay, copy
you right click.
There we go. Back into the playbook going to insert mode and all I will do here
is I'll just
paste in this particular command. Now what I want to now do is I want to
register the output as a
variable so we can actually use it. And let's just save this as free memory. So WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
variable so we can actually use it. And let's just save this as free memory. So
right now what I'm
going to do here is I'm going to just print the value and I'll use the Ansible
built in debug module
and let's pass in the message with my double quotation marks and my double
curly braces and
I'll just pass in the free underscore memory variable. Check this out. So let's
see how this goes in.
If I clear the screen, I say Ansible Playbook play 2.YML and to enter. Oh, of
course, let me go back
into the playbook. And for now going to insert mode and I'll just print the STD
out. Okay, so we just
get the standard out and not all this extraneous information. So let's rerun it WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
get the standard out and not all this extraneous information. So let's rerun it
again. Cool. So let's
look at what we see here. We have this string value. You see this with our
quotation marks. Now,
what we want to do is we want to get just this numerical value. So ultimately
we want to split
the outputs. Now effectively, there are three components that we can see here.
We have this part
here, then we have some space, and then we have this part here, and then again,
a little bit of space,
and then at the end, and then the size of the kilobytes. Now, what we want to
be able to do is to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
be able to do is to
actually isolate these three components or break them up. So we have access to
each one individually,
and then we can choose just the numerical value that we want to get. Now the
way we can do this is
by using a very particular method with thin Ansible. And this is known as the
split command. So let
me show you how this actually operates them. Okay, if I go back into my
playbook right here,
and when I take the STD out, if I just say split and then parentheses, I'm
effectively going to
split the output every time we incur some white space, which should break into WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
split the output every time we incur some white space, which should break into
three different
components. So let's see how this works now. Okay, run the playbook. Ah, look
at this. We actually
have an array object, see her square brackets. And inside of this, we have
three separate values.
We have the first element and the array, which is name free. We have the second
element, which is
the value. And then at the end, we actually have the units in this case here
kilobytes. So we have
this for the blue box as well as the yellow box. Now, because we actually have
an array object here,
recall how we actually access the constituent components, we access them via WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
recall how we actually access the constituent components, we access them via
their index position.
Now the very first position here is not actually position number one. It's
known as position number
zero. So zero is going to correlate to the word main three position one is
going to correlate to
the number which in this case here is five three oh four three six for the
yellow box and for the
blue box that would be four four nine one five two. And the second position in
the way is quite
simple. Just going to display the unit which in this case here for both devices WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
simple. Just going to display the unit which in this case here for both devices
is kilobytes.
What we want to do is we want to print out the second element, which is going
to be in
list position one. So let's see how this actually shapes out them. Let's go
back in right after we
split the output up. If we happen to go until it's maybe say this position zero
and run the playbook,
we just print out the word main three. Okay, then if we happen to print out
list position two,
you guessed it, what's going to happen? We're just going to see the word KB. So
by deduction,
if we grab less position one, we should actually get access to the numerical WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
if we grab less position one, we should actually get access to the numerical
value that we want to
use. So check this out. Bam, there is a value five to eight four twenty for the
yellow box and below
for the blue box four four eight two four four now recall just because these
look like numbers
doesn't actually mean they're going to act like numbers are still going to be
string values at
the moment. Say for example, I wanted to perform a mathematical operation on
this. So let's maybe
say I go into my playbook. Let me grab this information here. I'll just move on
to a new line for the
moment. And instead, I'm going to have a message. And I'll just say free memory WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
moment. And instead, I'm going to have a message. And I'll just say free memory
is below 100 meg.
And then what I'll do is I'll actually pass in the free memory value. Now
temporarily, let me
just comment this out for a moment. Okay, so no longer will this line have any
effects. And in fact,
before I do anything, let's make sure I'm printing the STD out. So right now if
I run this playbook,
we see here we're getting the message free memory is below 100 meg. And then we
actually see the
output of the value. Now the problem is right now is this line here is not WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
output of the value. Now the problem is right now is this line here is not
actually true.
The free memory as we can see here is round about 400 meg. And in this case
below,
round about 500 meg. So basically, we don't want this particular message to
print
on less that condition is true. So the keyword here is condition. So we want to
add in a conditional
statement so we can use the when keyword. So let's go back into play to. And
like I say,
we don't want to print this command all the time. We only wanted to print it
when a condition is
true. So here is what I want to happen. I'm going to grab this information here
, which we know is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
, which we know is
going to isolate the numerical value alone. And I'm going to say is less than,
let's say 100,000.
Now watch this if I happen to save this right here. And I run the playbook, we
're going to have a
problem. Watch this, let's hit enter, check this out right here, look at the
output. Let's grab
this here for the blue box. We get an actual failure. And it says the
conditional check free
memory actually failed. And it says the error was an unexpected templating type
error. So the issue
is the type pretty much we can't use this comparison operator between instances WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is the type pretty much we can't use this comparison operator between instances
of actual
answerable text, which is basically our string value we're talking about and an
integer value.
So the problem is here, as if we go back in, is that where's this looks to the
eye like it's a
number, this particular value. It really is just answerable unsafe text, ie, it
's just a string
value. And it's been compared to in this case here an actual integer. So what
we want to be able
to do is you want to take this number that we've parsed out, which is still a
string and then
convert it to an integer. And then we can run the comparison against another WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
convert it to an integer. And then we can run the comparison against another
numerical value.
So let's see how this is going to operate them. What I'm going to do is I'm
going to go into
insert mode here. I'm going to wrap this in parentheses right here. Okay. And
what I'm going to do is
I'm going to have a pipe symbol here. And I'm going to convert the value into
an integer by using
the int filter. So now let's check this out. Let's save this and see what
happens. Let's run play
to once again, let's hit enter. Aha, look at this right here. We don't get an
error. All of this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
error. All of this
happening is that we happen to be skipping the printing of the value because we
're not actually
below 100 meg of free memory. So just for the demonstration, what I can do is
let me go to my
blue box here. And what I will do is say pseudo DNF install stress hyphen
engine,
enter and type in my password here. Now what this is is a particular utility
that is used to test
your system. So let's say why and to enter. And what this is going to allow me
to do is it's going
to allow me to effectively choose the parameter that I want to really stress. I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to allow me to effectively choose the parameter that I want to really stress. I
can stress my
memory. I can stress my input output. I can stress my CPU, that type of thing.
So on the blue box,
what I want to do is I want to run this program to really crank up my memory
usage so that the
free memory end that's becoming lower than 100 meg. So if I say stress ng dash
dash VM two for two
workers, the more workers I use the more stress you put in the system, I'll say
dash dash VM bytes
for the amount that I want to be using. And let's try to use one gig of memory.
Okay, really stress WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the system. So if I open up a new tab and I say free dash h and to enter, we
can see here the
memory is gradually be getting lower. Let's run it again, getting lower again.
So let's try to run
our playbook right now. Let's add a what and to enter, look at this right here.
In the case of
the yellow box, because that was not stressed, we don't print out any value.
But in the case of
the blue box where we are running the stress test, we actually see here the
free memory is now below
100 meg and actually shows us here that the value is 77,000 k, which is again WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
100 meg and actually shows us here that the value is 77,000 k, which is again
below 100 meg. And
that is because we can now run the comparison between this particular value
that we passed out,
converted to an integer and compared it to, if we go back into the playbook,
then the
miracle value here that we specify, which is going to denote 100 megabytes. So
as you can see,
it's very, very important to understand that the output that answer will give
you,
whilst it may look like numerical values, they may not actually be integers or
floats, in which case,
you're not going to be able to run your comparisons until you perform your
conversions. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
conversions.