- Now that we've talked about the fail module,
we should also briefly cover the assert module.
So the assert module is a module that you can use
to show a message on success as well as on failure.
It's a bit like the fail module
but it has more advanced options.
Let me show you.
So the first playbook I want to show you is assert size
but the wrong version of it.
I made it easy to recognize which one is working,
which one is not, because sometimes I like
including playbooks that aren't doing
what you expect them to do.
So what this is doing, this is using VARs prompt,
I'm going into local host because there is no reason
to actually go out to another host.
And then the playbook is checking if file size is valid
and in order to do so using assert, so assert that.
And then you can list a number of conditions.
Condition number one, file size smaller than 100.
This is an integer, right?
It's not between quotes,
then we have file size greater than one.
We do a fail message file size must be
and we do a success message.
In case of failure, it will abort,
in case of success, it will continue.
And if it continues, it creates a file with this size.
Notice that it's using bytes instead of megabytes,
who cares about the actual size.
But what matters is the assertion.
So let's run it, ansible-playbook on assertsize-wrong,
and let's give it 50.
That should be okay.
And what are we getting?
Oh, that is interesting, we are getting
the conditional check file size smaller than 100 failed.
The error was unexpected templating error
and not supported between instances of string and integer.
Now what is the thing?
The thing is that if you enter a value using VARs prompt,
we get a string.
If you want to do greater than smaller than operations
on a variable, it needs to be an integer.
And that is not the case here, the variable type is wrong
and that is why we need to do something in addition.
So let me show you the contents of assert size
so that we can investigate what is different in there.
Now what do we see?
We see that the variable is defined
but the variable is defined by int,
this is passing through the integer filter.
So this will make sure that the file size variable
is interpreted as an integer.
Notice that this filter is not changing
the contents of the variable,
it's just changing the interpretation.
And we do the same for the other file size
and now we can try again and that should be more successful.
And we also include this private, no,
no need to hide the actual results.
So there we go, I'm going for the 50.
And in the 50 we see file size is good,
let's continue, create a file and change localhost
and now I'm going for 5,000 which will be wrong
and there you go, the assertion is having it fail.
So that is what is really nice about assert.
Assert, a little bit like block and rescue and always
is just another way of implementing something
that looks like an if then else statement.
And you're going to need them in Ansible as well
and that is what makes assert really cool.