Skip to content

Commit

Permalink
Make the extract filter return Undefined on KeyError
Browse files Browse the repository at this point in the history
groups['x']|map('extract', hostvars, 'somevar') would break if any host
didn't have 'somevar' set. With this change, it will return Undefined
instead. This change permits |map('extract', …)|map('default', 42) to
set a default value in such cases.
  • Loading branch information
amenonsen committed Aug 25, 2016
1 parent 2b3a22f commit 3ab9ddd
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/ansible/plugins/filter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ def extract(item, container, morekeys=None):
if not isinstance(morekeys, list):
morekeys = [morekeys]

value = reduce(lambda d, k: d[k], morekeys, value)
try:
value = reduce(lambda d, k: d[k], morekeys, value)
except KeyError:
value = Undefined()

return value

Expand Down

0 comments on commit 3ab9ddd

Please sign in to comment.