diff --git a/changelogs/fragments/fix_flatten.yml b/changelogs/fragments/fix_flatten.yml new file mode 100644 index 00000000000..387033d7e1c --- /dev/null +++ b/changelogs/fragments/fix_flatten.yml @@ -0,0 +1,2 @@ +bugfixes: + - fix flatten to properly handle multiple lists in lists https://github.com/ansible/ansible/issues/46343 diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 17309dd8aa5..759e6ce7c97 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -438,8 +438,8 @@ def flatten(mylist, levels=None): if levels is None: ret.extend(flatten(element)) elif levels >= 1: - levels = int(levels) - 1 - ret.extend(flatten(element, levels=levels)) + # decrement as we go down the stack + ret.extend(flatten(element, levels=(int(levels) - 1))) else: ret.append(element) else: