Make the extract filter return Undefined on KeyError

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.
pull/15928/merge
Abhijit Menon-Sen 8 years ago
parent 2b3a22fdba
commit 3ab9dddb3a

@ -363,7 +363,10 @@ def extract(item, container, morekeys=None):
if not isinstance(morekeys, list):
morekeys = [morekeys]
try:
value = reduce(lambda d, k: d[k], morekeys, value)
except KeyError:
value = Undefined()
return value

Loading…
Cancel
Save