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
added flatten filter #33102
added flatten filter #33102
Conversation
|
||
{{ [3, [4, 2] ]|flatten }} | ||
|
||
Flatten only the first level of a list (aking to the `items` lookup):: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo? aking -> akin
lib/ansible/plugins/filter/core.py
Outdated
elif levels is None: | ||
ret.extend(flatten(element)) | ||
else: | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems wrong to me.
Consider levels=1
. You decrement that to levels=0
, then the if levels >= 0:
branch gets taken and you recursively call flatten(element, levels=0)
. Here you find a nested subelement that is a list, so you check if levels:
which is False, then you check if levels is None
, which is also False, so you break, truncating mylist
at the 1st item that happens to be a list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, not sure how to check that initial input in levels is non 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mgedmin should be fixed now
SUMMARY
filter that flattens ... much easier to use than the lookup and has a 'level' interface
ISSUE TYPE
COMPONENT NAME
filters/flatten
ANSIBLE VERSION