- In this video, you will learn about Ansible facts.
Ansible facts are system variables that contain information
about managed system.
And each playbook starts with an implicit task
to gather facts unless you specifically disable that.
To use fact gathering from an ad-hoc command,
that's also possible, you use a setup module.
And additional modules for fact gathering are available,
like package_facts.
These additional modules allow you to gather facts
for no matter which assets you are managing with Ansible.
If you want to disable fact gathering,
use gather_facts: no in the play header.
And that might be interesting
because gathering facts takes time.
And if you don't want to wait the time to gather facts
that you are not going to use anyway, just disable them.
We are going to have a look at gather_facts.yaml
to see how it is working.
Let me demonstrate.
So I would like to start with Ansible on
ansible1 -m setup.
And let me redirect that to ansible1_facts.txt.
So this is how we manually get effects.
And I want to write that to a text file.
As you can see, it takes a little while
so that the next time we are referring to these facts
we can easily get them from the text file.
So there we go. We have this ansible1_facts.txt,
and here we can see the Ansible facts.
So ansible1, that is the host name.
And there, you can see that all the effects are stored
in one big variable with the name ansible_facts.
Within that variable we have the second tier variables
like ansible_all_ipv4_address
and these variables have their own value.
And if you look through them, then you can see
that the facts are really categorized
in different variables.
Like here, ansible_default_ipv4,
which contains all the information about IP addresses
or ansible_devices, which contains information
about the block devices that you are using.
And the nice thing about all these facts
is that these facts are variables.
And again, you can use these facts in conditionals.
That's exactly what we are going to do later.
Now let's have a look at the gather facts playbook.
So if you have a look at that,
then you can see that it's a really
simple playbook where nothing really is happening.
And that is because fact gathering,
if it is not disabled, is implicit.
So I don't have to run the setup module or anything
that is only something that you do
if you want to manually get effects.
I am just using the debug module and using the debug module
will print the value of the variable ansible_facts.
Let's run it.
We can ignore the message about a tower host,
which is not currently up and running.
It's not really important.
The important thing is what we see here
which is basically the same thing as what we have just seen.
These again, are all these Ansible facts.
But, and we need to scroll up a little bit
in order to see the difference
with the setup module that we've used before.
If you look closely, here we have the ansible2 host.
You can see that it's still ansible_facts
and it has variables like all_ipv4_addresses
and all_ipv6_addresses and ansible_local
and apparmor and architecture.
And if you compare that to the output of the setup module
then you can see that in the setup module
that we show you again, in the setup module output
we have a slightly different approach.
And that is where all these tier two variables
have ansible underscore in front of them.
Something strange is going on here.
In fact, the setup module is still compatible
with the old approach.
Whereas the fact gathering is showing you the new approach.
What exactly is the difference
between the old approach and the new approach?
Well, before Ansible version 2.5,
and we are talking about year 2019,
or 2018 something like that,
facts were not stored in one big variable ansible_facts.
They were just stored
in the variable ansible_all_ipv4_addresses.
Then in Ansible 2.5, they decided to put everything
in one variable ansible_facts
and to drop the name, the prefix ansible underscore.
Now the thing is that
the setup module is still printing this prefix.
And if you use fact gathering, you don't see that prefix.
And that prefix actually makes sense in the different ways,
how you can refer to your variables and facts.
That is what I'm going to explain in the next video
because it's a little bit confusing.