While working with some of the filters that have an option to pass in additional arguments, it wasn’t very clear to me how to pass in the arguments within an Ansible playbook.
For example, I wanted to use the trim filter to remove specific characters from a string I was working with.
By default, the trim filter strips leading and trailing whitespace from the string provided.
Now, let’s say you want to trim the "*." characters from our example above.
But what does this translate to when using it in Ansible?
It turns out that when working with the Jinja filters, the value to which you’re applying the filter is automatically passed on to the filter as the first argument.
Therefore, the following snippet in Ansible:
gets translated to this (in Python):
So if we want to trim "*." from our string, we need to ensure that the Python code that gets executed is:
In Ansible, this becomes:
So the snippet to trim this from our string in a playbook becomes:
which yields:
The same principle applies to other Jinja filters; for example, the int() filter, which converts a given value to an integer.
int Jinja Filter Method Signature
Ansible Task Snippets
Ansible Output
In this blog post, we demonstrated how to use Jinja filters that have multiple arguments, within Ansible.