So friends in the previous video we discussed the concepts of partitioning as
well as some
of the partitioning tools we can use, namely the part of utility.
Let's now see how we can use this using Ansible.
So where are we going to go?
You guessed it, we're going to go to the big old fashioned Google machine and
we're going
to search for Ansible partition disk.
Now, you'll notice at the very top we get this particular link here, it says
community.general.partum
module.
If we click on this, we can see here from the fully qualified collection name,
this is
not a part of Ansible core. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
not a part of Ansible core.
So we're going to have to install it with our good old faithful command, Ans
ible Galaxy
collection install and then specify the name of the collection.
Now, if you have been following along from an earlier scale, we should already
have this
particular collection installed, but in the event that you have not, then just
copy this
command as such, go to your terminal window on the control machine and just hit
enter.
As we can see here, I already have this particular collection installed.
Now, let's go back and look at the documentation here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now, let's go back and look at the documentation here.
So scrolling on down, we can see the brief synopsis of what this tool is going
to do,
but kind of know what it's going to do.
But nevertheless, as it says here, it's going to allow us to configure a block
device partition
and it's going to do this using the part of command under the hoods.
So check out, if we look at the parameters here, we're going to have some
important ones
here as one, which is very, very important.
You can see here directly because what does it say it says it is required,
meaning that
the module itself will not work unless we supply this particular value. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the module itself will not work unless we supply this particular value.
Now, this happens to be the device parameter.
We're going to supply a string value and that is simply going to be the block
device that
you want to target.
So remember on the previous video, we happened to manually use the part of
utility.
We targeted a very particular device, which was dev/ and it was NVMe0n2.
I believe pretty much in my case, this is the value that I would supply to this
required
parameter.
Again, if you happen to have a different value, do not sweat it. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Again, if you happen to have a different value, do not sweat it.
Just provide the value on your particular system.
Notice we have this option here, FS_type.
This is pretty much where we can supply the type of file system that we want to
give to
this particular partition.
So remember in the previous video where we happened to use the part of utility,
in my
case we can specify a particular value, the default I believe was EXD2.
In my case, because it was a railway system, I chose to use XFS, but pretty
much with
Ansible by providing that particular value to this particular parameter, we can WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Ansible by providing that particular value to this particular parameter, we can
do that
particular operation this time using Ansible.
Notice if we so wish we can specify a label to use.
So again, going back to the previous video, we happened to supply the GPT style
label
to the partition.
That would allow us to, in effect, create that GPT style partition.
But if we so wish we could use, as it says here, the default value in this case
here,
MS_DOS.
This is what we would use for MBR style partitions using the part of command. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This is what we would use for MBR style partitions using the part of command.
And if we happen to do so with Ansible, this is how we would invoke that
particular behavior.
Now this one here is your rather important parameter also.
This is the number parameter.
Notice this is going to take an integer value pretty much.
This is the partition number that we want to specify.
So in the case here, if we're just going to have one particular partition, we
can specify
partition number one.
If we happen to have, let's maybe say three partitions, maybe we want to target
the second
partition or the third, whatever it may be, we specify that information via
this particular WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this particular
parameter.
Important to note, by the way, is that this happens to be required when
performing any
action on the disk except for fetching information.
So if you're going to be creating some brand new partition, you want to supply
that partition
number.
Now this one here is also an important option.
We have part_end.
Again, we supply a string value.
Now it says here, this is where the partition will end as offset from the
beginning of the
disk.
Pretty much how big do we want the partition to be. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Pretty much how big do we want the partition to be.
Now we can do this in two different ways.
We can specify that maybe we want to use in the case here as per the
documentation.
We would like the partition to end after 10 gig or just like we saw in the
previous video,
we can actually specify a percentage of the disk to use.
And the previous example, we use 100% of the disk which utilized all of the
disk of course.
But in the example here as per the documentation, we can specify a number such
as let's say
15% or if you want 25% or 50% that type of thing. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
15% or if you want 25% or 50% that type of thing.
Notice if you do not supply a particular value, the default value assumed is
going to be 100%
of the disk will be used.
We also have the option of part_start which is pretty much the inversion of
what we just
saw before.
This is where the partition offset is going to begin.
Similarly, as we saw before, we can specify the value in gigabytes if we saw
which or
as a percentage noted to default value is 0% pretty much the beginning of the
disk if
we don't specify otherwise. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we don't specify otherwise.
We also have the state option right here.
And this is how we actually decide if we want to create a partition or if we
want to remove
a partition, if we want to remove it, well, we can set the state to absent and
that will
specify the particular partition we have used via the device via the partition
number and
then simply remove it.
However, if we want to create a partition, we set the value to be present which
in our
case here, this is what we're going to do.
So these are just some of the available parameters as we can see in the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So these are just some of the available parameters as we can see in the
documentation.
But as you know and can expect as always, there are more parameters and if you
so wish,
then you are free to explore them.
But now that we have a decent grasp about what the options are, let's go and
begin writing
a playbook to actually automate this particular task.
So let's go back to the control node and what I will do here is I will create a
playbook
called play1.yml.
So the very first thing I will do is go into insert mode, I'll have my three
hyphens at
the top to denote my yaml file and I'll give this playbook a name simply called
play1. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
play1.
Create a partition using partition.
Now this is where I want to specify which hosts in my inventory do I want to
target.
Now for a start, let me just say the host is going to be the blue box.
After all that is where I added the new disk to be targeted.
Now in this case here, what I will do here is I'll set gather facts to be false
and now
what I can do is begin defining my task.
So the very first task is going to be called task1.
Let's just say test task.
Okay.
At this point here, what I'm going to do is to provide the module that I want WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
At this point here, what I'm going to do is to provide the module that I want
to use.
Now what is the module actually called?
Well, the fully qualified collection name is community dot general dot parted.
And now we're going to supply the particular parameters that we just saw within
the documentation.
So one particular parameter that is required is the device parameter case.
We're going to make sure that is specified.
What is it we want to target now?
We're going to look at the blue box.
And like I say, it's going to be this particular disk right here, dev nvm e0 n2 WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And like I say, it's going to be this particular disk right here, dev nvm e0 n2
once again,
use the value as required on your own system.
So all I will do is I will paste in that particular value right here and target
that device.
Now the next thing I'm going to do is I'm going to specify which partition
number that
I want to specify.
So we use the number parameter and again, remember this is going to take an
integer value.
So in my case here, I want to target the first partition that is going to be
the partition
that I'm going to create.
Now like I say, am I going to create it? WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now like I say, am I going to create it?
Am I going to delete it?
Well, I can specify that via the state parameter.
And in this case here, I'm going to state that the state should indeed be
present.
Now what I'm going to do here is specify when should the partition actually end
now again,
we can specify this in two ways.
We can say this is going to end after one gig if I so wish but in my case here,
I just
like to do as a matter of percentage and I will just say it's going to end
after 100%
of the disk space has been used.
So check this out. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So check this out.
So what I will do here is I'll press escape colon wq to write and quits.
I will just ping my blue box to make sure I have connectivity, which I do.
And with that now done, all I'm going to do here is say answerable playbook
play one
dot yml and to enter.
And as we can see here, we do have a change on the system.
So with that now done, if I go back to my blue box.
If I now arrow up and run the part of the list command once again, type in my
password
for the super user.
Now we can actually see here we have these values populated. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now we can actually see here we have these values populated.
We have partition number one as we specified.
We have a primary partition and we can see the size of the partition is we have
used the
desk and its entirety 100% of that particular disk.
Now let me just show you something here.
Let's just say I wanted to run this particular command against both of my
devices.
Now what I'm going to do here is I'm going to delete that particular change
right now
just to undo it.
So I'll say pseudo part of dev nvm e0n2 to target that particular device.
And I'm going to say Rm1 to remove the partition and then select quits. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And I'm going to say Rm1 to remove the partition and then select quits.
If I relist, now we can see here all of that information is now cleared.
Check this out then.
If I go back to my actual playbook here and all I'm going to do here is let me
just copy
this over and I'll create this one call play two dot yml.
I think when you play two and I go into insert mode and I change the hosts to
actually be
the all group.
Now what do you actually think the problem might be here?
Well, if you can remember we have indeed created this particular disk here on WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Well, if you can remember we have indeed created this particular disk here on
our blue
box but it doesn't actually exist on the yellow box which is now as per the all
instruction
also going to be targeted.
So we're going to run into a little bit of an error here.
We're going to see how we're going to handle that.
So I'll just write quits.
So let's try to run playbook number two this time and to enter.
And we can see here we have a change on the blue box but we also actually have
this failure
right here.
Now check this out from the output here it says Ansible Facts. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now check this out from the output here it says Ansible Facts.
Error.
There was not a device called dev nvm e0n2 on that particular system.
So think about it like this.
We have this playbook that we only want to execute against this particular
device.
If that device actually exists how then are we going to solve this particular
problem?
Well we know from previous scales we can actually invoke conditional logic to
do such
a thing.
So the challenge for us right now is to determine how do we know if a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So the challenge for us right now is to determine how do we know if a
particular system has this
particular device available to it?
Well, let's find out.
So what I'm going to do here is I'm going to invoke Ansible directly from the
command
line and I'm going to target the blue box.
We know that particular device exists on that box and the module I want to use
is going
to be the Ansible built in setup module.
If I just hit enter and give it a moment it's going to collect the facts about
this particular
device and then we'll examine the output.
Okay, so we get all the information back right here.
Okay.
Now if we scroll on up to the very top right now we can see here we have this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now if we scroll on up to the very top right now we can see here we have this
outer key
Ansible underscore facts and everything else is going to be nested within.
Now we see all these other keys such as Ansible all IPv4 addresses and that
lists the IPv4
addresses, that type of thing.
What we want to do is to find the key that is going to find information about
the devices
attached to that particular system.
So the actual key we're going to look for is the devices key.
So let's just scroll down a little bit here.
To check out this part here Ansible devices, this shows us all the attached
devices.
So let's go to the next keys within, scroll on down. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let's go to the next keys within, scroll on down.
Next we actually have keys here correlating to the device themselves.
In this case here we have NVMe 0n1.
The one we want to see is the presence of NVMe 0n2.
Now if I scroll on down here, check this out right here.
This does actually exist on the blue box.
So in fact what I could do is if I want to take that output and just grab it
for NVMe
0n2 and to enter, we can see here we're actually getting output for that
particular device to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
particular device to
the blue box whereas if I scroll on up and I try to create for the same
information on
the yellow box, well this is not going to be present and that is actually the
problem
we're experiencing.
And we can see the module runs and we have no output shown.
So really what we want to do is we want to go into our playbook here and we're
going
to add that conditional statement.
So remember at the same level of indentation as the actual module we want to
use here,
we will have the when keyword.
Now what is it we're going to target here?
Well I'm going to say Ansible_Facts.
Now I want to parse out the particular keys that I'm looking for. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now I want to parse out the particular keys that I'm looking for.
So I will say Ansible_Devices and then the key within is going to be the NVMe
key 0n2
and I want to add the condition that that key is defined in the output.
So if we happen to find this particular key in the presence of the Ansible_F
acts, then
this particular command, this particular module should I say is going to
operate.
However in the case of any host whereby this particular line of keys is not WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
However in the case of any host whereby this particular line of keys is not
present,
I eat on the yellow box, this part here is not going to execute.
So one thing I'm going to have to do here is the fact that I'm relying on Ans
ible_Facts,
I actually want to make sure that we're gathering them.
So what I will do here is I'll go up to the top and I'm going to remove the
gather_Facts=false
and make sure we're actually going to collect them.
So let's check this out right here.
Now on my blue box once again, I'm just going to go into Parted and I'm just
going
to say RM1 to remove that partition and if I list it once again, now the
partition has
been cleared.
Now check this out. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now check this out.
If I say Ansible_Playbook, play 2.YML and to enter now, we'll gather facts
about the
machines and then run our conditional statement to determine whether we should
use the module
against that particular device or not.
So check this out, this might be a little bit unexpected here.
It says here we are apparently skipping both the blue box and the yellow box.
That doesn't really make sense.
So as we would expect it to target the blue box but skip the yellow box.
Now the reason being is a little bit of a peculiarity.
When we happen to use the Ansible setup module as we did via the command line WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
When we happen to use the Ansible setup module as we did via the command line
right there,
the keys nested within Ansible_Facts are all going to be prepended with the
keyword
Ansible_.
So that means all the nested keys such as the key representing the devices in
the output
that we just saw actually say Ansible_Devices.
However, when we want to access this particular key within our playbook, we
just specify the
keyword devices.
We don't actually add on this Ansible_ Is that a little bit confusing? WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We don't actually add on this Ansible_ Is that a little bit confusing?
Is that a little bit annoying?
Yeah, kind of but that definitely is the way we've got to work.
So with that said, let's make the necessary modification.
So let's go into the playbook once again.
And like I say, when it comes to the keys inside, all we want to do is remove
Ansible_HeroCare.
So now we should actually correct the target, these particular keys.
So let's save this and we will rerun the playbook once again.
So let's hit enter, give it a moment, we'll gather the facts.
And as we can see here, we have a little bit of a different outcome.
The outcome that we actually hope for unexpected, we have made a change on the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
The outcome that we actually hope for unexpected, we have made a change on the
blue box because
we actually have the presence of the dev NVMe0N2 device.
Whereas on the case of the yellow box, as opposed to having some type of fatal
error,
we are just simply skipping that box and not attempting to use that module on
that particular