So friends with our new found super power, how about we begin exploring some
additional
and simple modules directly via the command line.
So what we're waiting for, let's get to it.
So let's go in and have a look at the listing of our modules right here.
And the first one I'm going to have a look at right here is this one there, the
STAT
module.
As it says here, this is going to allow us to retrieve file or file system
status.
So let's just grab it and copy it.
And I'll press Q2quits, let's just clear the screen and we'll just say Ansible WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And I'll press Q2quits, let's just clear the screen and we'll just say Ansible
- Doc
and paste in the option for STAT.
So as we can see this is going to retrieve facts for a file system similar to
the Linux
or Unix STAT commands.
So scrolling on down we can see the path parameter has an equal sign next to it
that tells us
this is a require parameter and this is going to be the full path of the file
or object
where we want to get the facts.
So what I will do here is I'll create a new playbook.
So let's create Play4.yaml.
Over three hyphens, give it a name.
So we play for testing the STAT module. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So we play for testing the STAT module.
What we're going to do, we're going to target the all group.
We'll have gather facts set to false and now we define our tasks.
So I'll just say run the STAT module.
So what we'll use, we'll use the Ansible built in.stat module and the require
parameter
is going to be the path.
So in this case here I'll test for the temp directory and we're going to check
for a file
called CBT file.txt.
Now when I actually run this particular command I want to capture the output so
I'm going
to use the register option and I'll just call this STAT_result. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to use the register option and I'll just call this STAT_result.
So for now all I will do is I'll create a new task and I'll just say print the
result.
And I'll use the debug module with the var parameter and the variable I'm going
to print
is going to be STAT_result.
So what I'm going to do here is I'm going to create this particular file on the
blue
box but not have it exist on the yellow box.
So I will say touch CBT file.txt so now it exists right here on the blue box
but not
the yellow.
Now watch what happens.
Let's go and run this Ansible playbook.
So I'll say Ansible playbook play 4.yml and to enter and look at this right WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So I'll say Ansible playbook play 4.yml and to enter and look at this right
here.
The STAT module is getting information about the file that we specified.
So look at the yellow box.
We have this option here for STAT and it says exists and the value is false.
That is because it doesn't actually exist on the yellow box.
However if we scroll on up to the blue box right here the output is a little
bit different
a lot more verbose.
The STAT option has all of these other attributes right here and if I scroll on
down let's go
to the exist option. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to the exist option.
We see the value is true.
That is because that particular file exists on the blue box.
So here's what I could do is I could use this information to create a file if
it does
not exist on a device.
So let's go into play 4.
As opposed to just printing the STAT result we actually want to evaluate some
of the information
we've gotten back from that.
So remember the format of the output within STAT result and let me just go back
a second.
The Kiwi grab is going to be the STAT key and then we can grab the exist key
and we'll
determine what that value is if the value is true then we're going to skip over WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
determine what that value is if the value is true then we're going to skip over
it and
if the value is false we actually want to invoke the creation of the file on
that particular
machine.
So let's go back in.
And all I will do is I'll remove the information here and I'll just create the
file if it doesn't
exist.
So what we'll use is the Ansible built-in command module and the command I'm
going to run is
simply going to be touch forward slash TMP forward slash CBT-file dot TXT. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
simply going to be touch forward slash TMP forward slash CBT-file dot TXT.
Now I only want to run this in a particular condition and that particular
condition is
when we grab the STAT result I'm going to the STAT key and then going to the
exist key
if that value equals false.
Basically when we run this command if any of the devices do not have it present
we're
going to create it open.
I've just noticed I have an error that should be built and not built in and
lastly let me
just make sure this key is correct that should be exists with an S that is very
important
that we get the keys correct and rerun the playbook. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that we get the keys correct and rerun the playbook.
So check this out we ran the STAT module and when it came to creating the file
we skipped
over the blue box because it already existed in the case of the yellow box
though we did
create that particular file so if I go back and let's log into the yellow box
let's first
gain this and if I do an LS now we can see the CBT file has been created which
means
if we go back and we rerun the playbook once again it will now exist therefore
when it
comes to the creation we just skip over the task for two devices now.
So that is the basics of using the STAT command if you want to learn more we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So that is the basics of using the STAT command if you want to learn more we
know the place
we can go to is directly via the terminal via the Ansible documentation.