diff --git a/changelogs/fragments/from_yaml_all_filter_plugin.yaml b/changelogs/fragments/from_yaml_all_filter_plugin.yaml new file mode 100644 index 00000000000..bfae5577d5c --- /dev/null +++ b/changelogs/fragments/from_yaml_all_filter_plugin.yaml @@ -0,0 +1,5 @@ +--- +minor_changes: + - Added the from_yaml_all filter to parse multi-document yaml strings. + Refer to the appropriate entry which as been added to user_guide + playbooks_filters.rst document. diff --git a/docs/docsite/rst/user_guide/playbooks_filters.rst b/docs/docsite/rst/user_guide/playbooks_filters.rst index 826a741690b..91351564540 100644 --- a/docs/docsite/rst/user_guide/playbooks_filters.rst +++ b/docs/docsite/rst/user_guide/playbooks_filters.rst @@ -47,6 +47,22 @@ for example:: - set_fact: myvar: "{{ result.stdout | from_json }}" + +.. versionadded:: 2.7 + +To parse multi-document yaml strings, the ``from_yaml_all`` filter is provided. +The ``from_yaml_all`` filter will return a generator of parsed yaml documents. + +for example:: + + tasks: + - shell: cat /some/path/to/multidoc-file.yaml + register: result + - debug: + msg: '{{ item }}' + loop: '{{ result.stdout | from_yaml_all | list }}' + + .. _forcing_variables_to_be_defined: Forcing Variables To Be Defined diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 1e533b52c26..7c1f5561afa 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -210,6 +210,12 @@ def from_yaml(data): return data +def from_yaml_all(data): + if isinstance(data, string_types): + return yaml.safe_load_all(data) + return data + + @environmentfilter def rand(environment, end, start=None, step=None, seed=None): if seed is None: @@ -600,6 +606,7 @@ class FilterModule(object): 'to_yaml': to_yaml, 'to_nice_yaml': to_nice_yaml, 'from_yaml': from_yaml, + 'from_yaml_all': from_yaml_all, # path 'basename': partial(unicode_wrap, os.path.basename),