diff --git a/changelogs/fragments/fix_flatten.yml b/changelogs/fragments/fix_flatten.yml new file mode 100644 index 0000000000..387033d7e1 --- /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 f6f6e28cde..21cf656a36 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -463,8 +463,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: