Add new split filter (#73532)

* Add version added
This commit is contained in:
Matt Martz 2021-02-09 09:37:15 -06:00 committed by GitHub
parent 1a2da990a4
commit d3441bc313
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 1 deletions

View file

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

View file

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

View file

@ -41,7 +41,7 @@ from random import Random, SystemRandom, shuffle
from jinja2.filters import environmentfilter, do_groupby as _do_groupby
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._text import to_bytes, to_native, to_text
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,
'items2dict': list_of_dict_key_value_elements_to_dict,
'subelements': subelements,
'split': partial(unicode_wrap, text_type.split),
}

View file

@ -608,3 +608,12 @@
- thing|quote == "''"
vars:
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"