Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new split filter #73532

Merged
merged 2 commits into from Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/split-filter.yml
@@ -0,0 +1,2 @@
minor_changes:
- Filters - Add new ``split`` filter for splitting strings
6 changes: 6 additions & 0 deletions docs/docsite/rst/user_guide/playbooks_filters.rst
Expand Up @@ -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 }}
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/plugins/filter/core.py
Expand Up @@ -41,7 +41,7 @@
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
Expand Down Expand Up @@ -662,4 +662,5 @@ def filters(self):
'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),
}
9 changes: 9 additions & 0 deletions test/integration/targets/filter_core/tasks/main.yml
Expand Up @@ -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"