Skip to content
Advertisement

Why it is possible to use a split filter in ansible?

It might look like a weird question by, I really would like to understand this. I am new to Ansible, and I am trying to understand how I could figure out that I can use a filter called split. As I understand, the Ansible filters are builtin inside Jinja2 engine. On the project website there is nothing about split filter. I am really confused how come, it is possible to use split filter.

On Ansible website there is one mention about split

https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html

To split a string into a list:

{{ csv_string | split(",") }}

My question here is, how would I know that I can do | split(",",1) that page does not explain that for example I could call a split filter with delimeter and a integer that describes how many times I want to split it.

Is there a different documentation, or a way to figure those things out on my own?

I was told that I could do in Python:

help(str.split) 

And this explains me how to use split method, this is great however, how can I be sure that this is the same way of implementation as in the Ansible filter? Is this split filter from Ansible documentation inside ‘Ansible’ or inside Jinja? And maybe its just missing from documentation in Jinja website?

Advertisement

Answer

This doesn’t explain where | split(",") is coming from but per the Jinja docs:

Python Methods

You can also use any of the methods defined on a variable’s type. The value returned from the method invocation is used as the value of the expression. Here is an example that uses methods defined on strings (where page.title is a string):

{{ page.title.capitalize() }}

So str.split() should also be valid syntax and work the same way.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement