Add new split filter (#73532)

* Add version added
pull/73540/head
Matt Martz 4 years ago committed by GitHub
parent 1a2da990a4
commit d3441bc313
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
minor_changes:
- Filters - Add new ``split`` filter for splitting strings

@ -1612,6 +1612,12 @@ To concatenate a list into a string::
{{ list | join(" ") }} {{ list | join(" ") }}
To split a sting into a list::
.. versionadded:: 2.11
{{ csv_string | split(",") }}
To work with Base64 encoded strings:: To work with Base64 encoded strings::
{{ encoded | b64decode }} {{ encoded | b64decode }}

@ -41,7 +41,7 @@ from random import Random, SystemRandom, shuffle
from jinja2.filters import environmentfilter, do_groupby as _do_groupby from jinja2.filters import environmentfilter, do_groupby as _do_groupby
from ansible.errors import AnsibleError, AnsibleFilterError, AnsibleFilterTypeError from ansible.errors import AnsibleError, AnsibleFilterError, AnsibleFilterTypeError
from ansible.module_utils.six import iteritems, string_types, integer_types, reraise from ansible.module_utils.six import iteritems, string_types, integer_types, reraise, text_type
from ansible.module_utils.six.moves import reduce, shlex_quote from ansible.module_utils.six.moves import reduce, shlex_quote
from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.module_utils._text import to_bytes, to_native, to_text
from ansible.module_utils.common.collections import is_sequence from ansible.module_utils.common.collections import is_sequence
@ -662,4 +662,5 @@ class FilterModule(object):
'dict2items': dict_to_list_of_dict_key_value_elements, 'dict2items': dict_to_list_of_dict_key_value_elements,
'items2dict': list_of_dict_key_value_elements_to_dict, 'items2dict': list_of_dict_key_value_elements_to_dict,
'subelements': subelements, 'subelements': subelements,
'split': partial(unicode_wrap, text_type.split),
} }

@ -608,3 +608,12 @@
- thing|quote == "''" - thing|quote == "''"
vars: vars:
thing: null thing: null
- name: split filter
assert:
that:
- splitty|map('split', ',')|flatten|map('int') == [1, 2, 3, 4, 5, 6]
vars:
splitty:
- "1,2,3"
- "4,5,6"

Loading…
Cancel
Save