- In this video, you will learn how to use filters
to change the way that variables are interpreted.
So, filters allow variables to be processed
before using them in Ansible.
So, you have the variable,
the variable is treated with the filter
and after applying the filter to the variable
that is what you are going to work with.
And I'm explaining it that way to make sure you understand
that filters do not change the value of a variable.
They only change how the variable is being used.
In order to use a filter,
you put the variable between curly braces, it's normal.
You use a pipe and behind the pipe
you identify which filter is going to be used.
I have two examples and I think we should just check out
these examples before continuing.
So, the examples are in the filters
subdirectory in the Git repository.
And to start with I will show you int-filter.yaml.
So, what do we see?
We see a simple playbook
which is running a variable prompt
and a variable prompt is prompting for a variable.
Let me use private now so that we can actually see
the text that the user is entering.
Then within that playbook the assert module is used
and the assertion is taking two assertions,
so both of them must be true.
And in the assertion, we have file size pipe int.
So, file size pipe int means
that file size is going to be interpreted as an integer.
And here also file size pipe int
means that file size must be interpreted as an integer.
And you know what, let me clean this up a little bit
and let me remove the parts about the filter
so that you can see that there is a reason
why this filter is actually being used.
So, I'm going to run ansible-playbook int filter,
and I'm providing a valid value like 50,
and, oh, there we go.
And I am getting unexpected templating type error,
smaller than equals,
not supported between instances of string and integer.
That is why we need to filter.
The default interpretation of this variable is wrong.
So, I need file size pipe integer
and just to make sure that it is behaving the right way
in the when statement, I'm putting the variable
including the filter all between the braces.
So there we go and if you run it again this time,
I am typing 50, then we can see that the file size is good
and that is working out alright.
We don't really care too much about the error message,
that is out of scope.
That is not what this playbook is about,
trying to create a file in the roots directly
without privilege escalation, that's not working, who cares?
It's the filter that we care about.
I have another example which is storage filter.
And in storage filter
we can see it's a little bit more complex.
The core of what it is about is right here.
So, here in the highlighted line
you can see that set fact is used.
And set fact is used to define
a new fact with the name vgsize.
And in order to define this fact
it is looking at the ansible fact.
This is the ansible fact.
The ansible fact is printing the size
of a volume group in gigabytes,
but unfortunately by default
that size is printed as a string as well.
This is a string value.
And in order to do greater than or smaller than operations
we need it to be an integer value.
Notice that really it's not really necessary
to do the pipe int here, but I like having it here
because it makes it clear what is happening.
It is required though, to use it in a when statement
because there you really need vgsize
to be interpreted the right way
to do whatever needs to be done.
I'm not going to run this one
because this playbook requires a lot of dependencies.
I just wanted to show you two use cases
of filters in action.
Let's go check out some more information
about filters in the slide.
So, here I would like to explore some common filters.
We have mandatory, mandatory as the play fail
if the variable does not have a value.
So, my_var pipe mandatory,
if you have not defined my var,
playbook execution will fail.
Default will set a variable
without value to a default value.
That's a little bit more elegant than mandatory.
You can add the optional True to also set the variable
if it has an empty string or a boolean False as its value.
So, we have my_var pipe default, myValue, True.
So, that is how you can make sure
that default values are signed.
Capitalize is what you can use to capitalize a string.
For whatever reason you need, uppercase use capitalize.
Int is what we have seen,
int will convert a variable to an integer.
Float converts a variable to a float.
And now we have the plus and the minus and the slash
and the multiplier which performs basic calculations.
That's an interesting one.
Here you see any example, vgsize pipe int minus one.
So, it takes the current value,
the current (mumbles) value of vgsize
and it's subtracted by one.
Then there's union, union creates a single list
that is based on multiple input lists.
There is random which extracts a random element from a list.
So, we have a, b, c, d, or a, b, c pipe random.
You will just get either an a or a b or a c.
Sort will sort a list in the appropriate order,
so that would be 2, 4, 6, 8 in this case.
And there's password_hash,
password_hash is a little bit more of a complex filter.
It'll generate a hashed password based on an input string.
And that is actually pretty important
if you want to create users
and you want to provide these users
with encrypted passwords.
There is also quote.
Quote is used to put the command output between quotes
so that it can be sanitized.
Let's go check out two more examples.
The examples are in a search filter and in password_hash.
Let me show you.
So, here we have a search filter.
So, what is a search filter doing?
This is doing an assertion,
the string that we've got is 2, 3, 1, 2, 4, 2.
First, we are doing a sort.
Then we are doing a unique, and then we make it a list
and we check that it is equal to the string 1, 2, 3, 4.
It's a little bit of a complex use of filters
as multiple filters are applied
and that's actually what I like about it.
So, ansible-playbook on assertfilter.yaml
and there we go, and yay, all assertions have passed.
And that means that the cleanup in this assert filter
was happening the right way.
Another pretty common filter is password_hash.
So, here we got it.
What this is doing?
This is creating a user with an encrypted password
So, vars prompt is prompting for the password.
And this time vars prompt,
it's nice to have private set enabled
so that nobody can see the password that's actually entered.
We create user Sharon
and then debug is using message on passw
and passw is processed by the filter password_hash.
And this password_hash in this case has two arguments,
sha512, which is the password_hashing algorithm
and myrandomsalt which actually is not so very randomsalt.
You can omit the myrandomsalt,
but I thought it's cool to show you myrandomsalt
so that we can actually see it in the result.
So, here I'm using register mypass.
So, we register the resulting variable
in temporary variable mypass.
Then I'm using debug to show you what mypass is all about
and we use create the user
which is creating the user with mypast.msg.
Apparently mypast.msg
and that's what we will see when we run the debug.
Mypass.msg contains the crypto-string.
And the crypto-string is exactly what you need
if you want to create users with secured passwords.
So, let's run it, ansible-playbook on password_hash.yaml.
And there we go, it's prompting for the password.
And then I have a missing pseudo password.
So, missing pseudo password,
that means that I need to put in a minus K.
So, we prompt for it.
So, there we go, which password?
Well, here is the password
and now it is working out alright.
So, what do we see?
This is a crypto-string.
The $6$ is for the encryption algorithm.
Myrandomsalt is the randomsalt
and the remainder of this string
is the hash of the encrypted passwords
and it creates the user with this encrypted passwords.
So, this is how you can use
a couple of pretty common filters
to treat how your variables are dealt with.