- This video is about Fact Caching.
So, after fact gathering, facts are kept in-memory
and stay there for the duration of playbook execution.
That is not very efficient, is it?
But it is what is needed
if you need your facts to reflect the accurate state.
If that's not needed,
you can use plugins to enable fact caching.
And when you use a plugin,
facts should be gathered manually or as a scheduled job
that is executed after expiration of the fact cache.
And next, all playbooks can run without fact gathering
and access their facts from the fact cache.
Until the cache has expired,
then you will get an error message
and then you will know
that you need to do fact gathering again.
I have listed all the steps that are required
to set up redis-based fact cache, let me show you.
So, to start with I'd like to show you,
ansible-doc -t cache -l,
where you can see all the different
caching mechanisms that can be used.
Definitely the easiest solutions are either based on redis
or on memcached.
Memcached is quite workable as well.
And hey, ansible-doc -t cache redis,
do we have any usable information?
Well, as you can see it's showing some prefixes,
but not really a very good documentation.
So, let me be your documentation.
To start with sudo dnf install -Y redis.
I'm doing that on a control note
and I'm doing that on a control note for a simple reason,
that that is where you are going to run your fact cache.
Of course, you can use a playbook as well to do this
but hey, this time I'm going for the quick solution
and I am just going to straight
install these different components.
So, sudo systemctl enable -- now redis
will start the redis in-memory database.
Redis in fact is a very simple in-memory key value store.
So, it's a database, an in-memory database
as simple as simple can be.
The next command is an important one.
We see sudo pip3 install redis.
This is where we are using redis
and we are adding redis functionality to Python.
In order to do so we need pip3 install.
You can safely ignore the warning that is shown here.
I do want this redis part
to be available throughout all my system.
So, I am fetching it using sudo functionality.
Now, this is interesting because you will notice
and you will notice it over and over again
in the next lessons that for enhanced functionality
you need to install additional stuff in Python
like right here, this redis stuff.
So, we need to wait for a little bit
until that is done and then we can continue.
So, it is done.
Let's configure this fact cache in ansible.cfg,
and in ansible.cfg you do your fact caching
under the defaults setting.
So, use fact caching is redis
and fact caching timeout
is 3,600 or 36,000 if you want, that really depends.
This fact caching timeout
in fact is a very important parameter
because this is the default timeout that you will be facing.
And by default that will be an hour.
If you want your fact to be more accurate, make it shorter.
If you want your fact cache to be valid for a longer time,
make it longer.
All depends on the dynamics that you want
from your fact cache.
Next, I need to specify fact caching connection
equals localhost:6379 which is the port :0,
where zero is the redis instance.
And that is what we need to do
in order to configure the redis plugin.
Let's get back to the slides
to learn how we are going to use this fact cache.
So, in order to use the fact cache,
first step is to make sure
that you have something in your fact cache.
Then you can use a checkcache.py script,
it is provided in the plugins Git repository directory.
And next you can use ansible-playbook checkfacts.yaml
to check how it is working.
Let me demonstrate this.
So, for your convenience in the Git repository,
there is a separate subdirectory with the name fact cache.
And in fact cache
we have everything that we need for this demo.
So, the first thing I would like to show you is checkcache.
Checkcache is a simple Python script.
And in this simple Python script,
we can see key ansible_facts + kubuntu.2.4 ansible 1.
This is going to work with
the ansible facts that have been cached for ansible 1
and we print ansible_memfree megabytes.
So, what do you think is going to happen
if I run it right now?
I am getting a non-type pretty curious error message
but the thing is that we are addressing
something that does not exist.
Now, I am going to use sudo systemctl status redis.
I wanna make sure that it is up and running, it is.
So, now I'm going to use ansible all -m setup
which is going to populate the fact cache.
And let me run this check cache Python script again
and there we go, we have a value, 123
and this is obtained from the fact cache.
That's the point I want to make here.
So next, we can have a look at checkfacts.yaml
and what do we see in checkfacts.yaml?
We see that gather facts is set to False
and debug anyways is going to use
the variable ansible_facts.
That's a very simple playbook
but it doesn't have to be any more complicated
to show you that fact catching indeed is working.
As you can see and wow, was that fast or wasn't it?
It sure was.
You can see that this playbook
is working from the fact cache.
Do notice that at some point sooner or later,
the fact cache will expire.
And if the fact cache is expired,
you will need to run your fact cache again.
So, it doesn't require a little bit of manual work.
Fact caching is cool,
if you don't need your facts to contain actual values
but there's also a couple of things to watch out for.
Let's go check that out.
So, while using a fact cache,
there's a couple of things to observe.
First, once the fact cache expires,
all playbooks that require facts
and have gather_facts:False will fail,
obviously because they will be facing undefined variables.
So, you might want to run a scheduled job
to refresh the fact cache automatically.
And to ensure that a playbook does not use cache,
but gather facts instead,
you can use ansible-playbook
whatever.yaml with --flush-cache option,
- -flush cache option allows you to use
the actual value of your facts.
And that is something you definitely have to do
if you have playbooks that depend on
for instance the amount of available memory,
that's a very dynamic value.
You really must make sure
that you are using the actual facts.