Okay friends, so think about it like this.
We're going to have to write a playbook
that is effectively going to gather factual information
about our devices.
Now what type of information are we going to have to get?
Well, we're going to have to get the host name, of course,
as well as the Linux distribution,
then the IPv4 address,
as well as the device's uptime and seconds.
Now this information can all be gathered
ultimately via the setup module,
but we want to effectively devise
which particular keys pull out
these very particular pieces of information. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
these very particular pieces of information.
So let's just go and try that first in.
What I will do here is I'll just say Ansible,
and let's just target one device for now
to keep the output a little bit smaller.
We'll say device number one,
and I'll just use the setup module here.
So if I hit enter here,
just give it a moment to gather all the facts
and check this out.
We see all of this information, a whole ton of it.
So let's scroll on up
and let's find out the keys that are actually relevant to us.
So I feel like at the key here,
Ansible default IPv4,
inside of this here, we also have an address key
that is going to give us the IPv4 address WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that is going to give us the IPv4 address
of that particular device.
So think about it that.
So let's collect that information straight away.
So inside of here,
what I'm going to do is paste this in
and then say dot address
that gets that piece of information.
If I scroll on down,
I see this key for Ansible distribution,
which returns red hat that is the distribution of the device.
So let's grab this particular key here,
and again, we'll paste this in right here.
If we grab the key,
Ansible_hostname,
that is going to return the actual hostname.
So let's just grab this one right here.
And lastly, we want to see the uptime in seconds. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And lastly, we want to see the uptime in seconds.
And as we can see,
we have this particular value,
Ansible uptime second.
So let's just copy this
and I'll get around that space here.
So let's just pop this one there.
Okay. So basically,
what we now want to do is to create a template
that's going to use this information
and effectively populate it into a text file on the device.
So let's go to our device right here
or rather our control mode, should I say.
And let's just call this playbook right
or maybe even let's say right facts.
Okay.
So let's go into the top,
have our three hyphens.
Let's give this playbook a name.
We'll just a gather information
and write details about devices. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and write details about devices.
So here we're going to target,
we'll target the all group.
In this case here,
we do actually want to gather facts.
Now by default, facts are going to be gathered
so you don't actually have to specify this explicitly,
but let's just do so for posterity.
And now I'll define my tasks.
So task here is going to be use a template
to write fail to this.
So what we're going to use,
we're going to use the Ansible_Belten template module.
And as we saw before,
we're going to have a particular source.
And the source here is going to be called,
let's just call this,
I don't know, device info.j2. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I don't know, device info.j2.
And the destination is where abouts
on the actual target do we want to write
this particular fails.
I'm going to use the temp directory.
And I believe the actual fails should be called
device_info.txt.
Let me just double check that.
Yep, device info.txt, that's the right one.
Perfect.
So that is all we need for the actual playbook,
but we do have to construct our template device info.j2.
So let's ensure we have our templates directory right here.
So let's go into this.
And let's just create this template called device_info.j2. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And let's just create this template called device_info.j2.
So let's just say my hostname is,
and then I'll use my double color braces.
And we'll grab the Ansible hostname right here,
paste it in.
We'll say my Linux distribution.
And again, let's go back.
Let's grab this particular key here.
Again, double color braces.
And we'll just paste this in.
Say my IPE4 address,
let's grab the default IPE4 address right here.
Again, let's have the double color braces
and just paste in our value.
I'll just say my current uptime is,
and then pass in the value with my color braces.
And we'll just copy in Ansible uptime seconds right here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And we'll just copy in Ansible uptime seconds right here.
Grab here and just paste.
And let's just add on the word seconds at the end,
because this is just going to give us an numerical value.
So let's just write and quit to save this.
Let's go back a directory to our playbook,
clear the screen.
And all we'll say is AnsiblePlayBook,
write facts.yml and hit enter.
So let's give it some time to gather facts about the devices.
Now the gather facts.
And we begin using your template to write the file to the disk.
So what I can do is go into my devices via SSH WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So what I can do is go into my devices via SSH
to actually verify this.
And in fact, instead of doing that, what I will do.
So I'll just use Ansible to target both devices
at once, I'll say AnsibleAll.
And all I will do here,
is I'll just attempt to cat the device itself.
So I'll say cat forward slash TMP.
And the file should be called device_info.txt
within the temp directory.
If I just enter here,
check this out right here for device number two.
Here is the actual information.
My hostname is device two.
My Linux distribution is Red Hat.
My IPv4 address is 192.168.160.137. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
My IPv4 address is 192.168.160.137.
And it says my current uptime is 9,678 seconds.
Then we go below to device number one.
And we get a different set of information.
It says my hostname is device one.
My distro is Red Hat.
My IPv4 address is 192.168.160.136.
And my current uptime is 9,481 seconds.