- In this video
we are going to explore callback and timer plugins.
The use of callback and timer plugins
is completely different.
It's more about performance measuring.
So callback plugins extend Ansible
by modifying how it respond to events.
And to do their work,
callback plugins modify the output of the ansible commands.
You can use ansible-doc -t callback -l
for a complete list,
and to get an impression of what exactly
you can do with these callback plugins.
And specify the name of the specific plugin
as an argument to ansible-doc -t callback
if you want to get help, use it.
If you are going to use callback plugins
you need to enable them
in ansible.cfg and in order to do so
you use the callback_whitelist setting
that looks like callback_whitelist
is timer, profile_tests, cgroup_perf_recap
if you want to include all of these callback plugins,
really it depends on which callback plugin you want to use.
Let's talk about cgroup_perf_recap.
What is cgroup_perf_recap?
Well, that is a callback plugin that allows you to
monitor detailed resource usage when running a playbook.
And you can use it to measure impacts
after changing the forks parameter for instance.
This is all about seeing how much resources
are spent where exactly.
Now the challenge with cgroup_perf_recap
is that it is based on Cgroups functionality.
And Cgroups allow for system performance tuning
and monitoring.
Cgroups is a generic Linux setting.
And that means that your Linux distribution
must offer support for Cgroups.
And that might to be configured
before you can use cgroup_perf_recap.
At this moment cgroup_perf_recap needs
Cgroups version one support.
The additional challenge is that
on recent Linux distributions like Fedora
Cgroups version two has been introduced.
And if you are on rail eight for instance,
there Cgroups version one is still the default.
Now, if you want to make sure
that you are using Cgroup version one
this is what you can do.
You can edit the etc default grub
on the control host that is using Cgroup version two.
And you use the parameter systemd_unified_cgroup_hierarchy=0
to the end of the line that loads the kernel.
And that will give you a presentation
of Cgroup output that is compatible with cgroup_perf_recap.
Do not forget if you make changes to the Grub configuration
you will need to learn sudo grub2-mkconfig
or whatever you need to do
on your distribution to rewrite Grub configuration.
This command works on Fedora.
Other distributions may need a different approach.
Now let me walk you through a demo
and in order to do so this time I have prepared a slide
because this slide contains a lot of information
and I wanted to make it easy for you
to run all of these commands.
Let me demonstrate.
So I need to start in the ansible.cfg
and in the ansible cfg in the defaults setting
I'm going to include callback,
callback_whitelist equals cgroup_perf_recap.
We need a corresponding setting.
And that setting is in an additional section
of the configuration file
which will be callback_cgroup_perf_recap.
And in there we need control_group equals ansible_profile.
Now thing is if you work with Cgroups
you need to create a so-called controller.
And in this controller
you are going to put whatever you want to measure.
And this defines the name of the cgroup controller.
You need Linux commands to create these cgroups.
And the command is cgcreates.yaml
Just to make sure that we do this Ansible style
here we can see how it works.
So we need to install Cgroups and in order to do so
we need to use become parameters on localhost
which is the control note.
And first we need to make sure that we install
the packets that contains the cgcreate binary
which happens to be libcgroup-tools
without libcgroup-tools insulation you cannot use cgcreate.
Then we create the Cgroups and I'm using command
and I know command is not a prettiest option,
but it's the only option if there is
no specific Ansible model.
So in this command cgcreate
I'm using -a ansible:ansible -t ansible:ansible
which is about the permissions I'm using ansible_profile
which is a Cgroup I want to create
and I want to put in three types of data.
These are the common types of data,
so don't worry about this.
Just make sure that you include CPU accounting,
memory, and PIDs so that you can work
with all of these,
and also make sure that you replace ansible:ansible
with the actual name of the user
that is going to use Ansible.
So ansible-playbook cgcreate.yaml is doing what?
It's generating an error.
That's an interesting one cannot open, blah, blah.
Why is it generating this error?
Well, in this error, you can already see
that it is trying to refer to the cgroups information
but the cgroups information does not yet exist.
Now the playbook is creating it.
So let's run it again and see what it is doing.
And there, we can see disappointing information.
It does show information, but not in the right way.
And that is because we need to run the playbook
using cgexec.
So cgexec -g cpuacct, memory, pids
ansible_profile ansible-playbook
on let's use cgcreate.yaml.
So what is this command doing?
All in this command we are telling cgexec
that it needs to run this specific command.
And the command in this case is ansible-playbook.
We need to run the ansible-playbook
within the Cgroup and the Cgroup is ansible_profile
and we are going to record data
about CPU accounting, memories and PIDs.
So here we go, we run it again.
And this time we do see information.
We do see information regarding memory,
regarding CPU usage, regarding the number of PIDs
for all of the different steps in the playbook.
So it's measuring this for every single task,
and this makes this a very precise tool
to measure exact performance utilization of your playbooks,
but do keep in mind, it only works
if you run your playbooks through cgexec,
without cgexec you won't see any result.
Now Cgroup functionality is pretty cool
but it involves a lot of work.
And sometimes you might want to measure performance
using something a little bit more lightweight.
And that is where timer can be interesting.
Timer just shows the duration of playbook execution
or profile_tasks which shows start time of each task
as well as how much time is spent
or profile_roles which is doing the same for roles.
And I'm going to demonstrate that as well.
In this demo we are going to start
by enabling these plugins.
And once they are enabled, they don't require anything else.
We can just run on them.
And that is what you are probably going to like about these.
So let me use the ansible cfg, that's where we start.
And I am just going to comment out the Cgroup information
and I am going to use a new callback_whitelist
and set that to timer comma profile_tasks
and profile_roles.
And then I am going to run my playbook again
without a cgexec this time.
And we can see information.
When did it start, how much time was created,
and we can see a nice summary
of how much time was spent everywhere.
Now, the thing is that these plugins are not as detailed.
They just show you current execution time
but in some cases, that might be enough.
So if you just want information
about time that was spent handling a specific task
have a look at timer, profile_task, profile_roles.
If you want detailed information about memory
use usage in every single part of the playbook
definitely go for Cgroups.
Both of them are very interesting
but it really depends on what you want to do.