- In this video, we are going to explore the use of blocks.
So what is a block?
Well, the block is a electrical group
of tasks and blocks are convenient
because you can use them to control how tasks are executed
in an easy way.
If you want a web statement to apply to multiple tasks
that's where blocks are coming in
blocks can also be used in error condition handling.
So you can use block to define the main tasks to run.
And then you'll use rescue to define tasks that run.
If tasks that are defined in the block are failing
and you use always to define tasks that will run regardless
of success or failure of block and ratio tasks.
So that makes a block can be used
like an if then else statement
you should notice that items cannot be used on blocks.
That's an inconvenience.
Do remember it when you start working with blocks
let's go check this out.
So in our first example,
this is a very simple example.
And in the simple example, we are running
against all hosts and we have a block.
As you can see the play.
Heather starts with tasks.
Then before you start actually specifying the tasks
there is this block section and below the block section
as child elements through the block
you'll find the actual tasks
the tasks themselves are not very interesting.
It's just installing and running a service.
The interesting part is that the
when statement applies to the entire block.
so when Ansible distribution is Rocky
that is how it's going to work.
Then the entire block is going to be executed.
So Ansible playbook on blocks dot YML.
It's showing us that everything is working now.
Let's also prove the negative.
That is something that I would like to advise you.
Anyway, if you write a playbook to do something
then it's nice to see
that the playbook is actually doing what you expected to do
but you should also test the opposite, meaning
that you should make sure that the playbook is
not doing it when the condition is not true.
So in my configuration where I only have Rocky machines
well, it's easy to put anything else.
And there we can see skipping
and skipping and that's exactly what we needed.
Those second example is blocks 2 dot YML
is a little bit more complex.
So what do we see?
We see a block, a rescue, and an always.
So block is what we want to do.
If the block tasks don't work
we are going to execute the rescue tasks.
And always we are going to learn this specific command.
I've kept it simple by using the command module.
So the command module is a removing a file command
usr bin RM var www.
Then it is creating a file
and then it is writing a message to, to the logs.
It said, is this best practice?
it's not let's change it.
Why would we want to use the command model?
If you want to remove a file, I mean, file
path or name if you want,
var WW, HTML, index dot HTML,
state absent
that will remove it as well.
And then we have this other command to create.
Get out of here.
We are going to use file again
and file is going to use the path.
So the path is TMP rescue file
and state touch touch will make sure that it is created.
If it does not yet exist.
We only cannot really change the
the logger command into an Ansible model.
So I will keep this logger command as it is.
Sometimes command is unavoidable, but Hey
it's a best practice.
If you can
you should avoid using command or shell even worse.
Let's go for Ansible playbook
blocks 2 dot YML and see how it is working.
So what do we see?
Well, it remove the file back to the playbook,
remove a file.
That's what it needed to do.
So the file at this point is removed, which means
that the rescue doesn't have to happen.
Now I'm going to learn it again.
Now we can see that removal a file is giving an okay and Hey
we don't see the rescue and you know why we don't
see the rescue?
That is because of the nature of rescue
rescue will earn tasks.
If tasks that are defined in the block are failing.
We don't have a failure on remove a file because
of the item, potent nature of the playbook.
So you know what it happens to be that using command.
Wasn't such a bad idea.
Anyway, so I am getting back to command
and we are going to rewrite command.
And what is this command?
Well, the command is removing, right?
So I'm using cmd RM var www and so on.
And then we are running it again.
So here we go, running it again.
And this time, yay
the command is failing and the file is created now
again, and this is an important thing.
In fact, that I wanted you to be aware of.
This is all about the item, potent nature of Ansible.
If you are using Ansible models
most Ansible models are item potent by nature.
So they will just give an okay
if no changes were were needed.
If you have command
and command is trying to remove a file that doesn't exist
then the command will give you a non-zero return code
which is what we see here.
Return code one.
That's obvious because the command does not exist.
That's a real failure.
And that is why in this second attempt
the rescue is going to be effective.
And in the first attempt, it is not.