servers/filter_plugins/util.py

28 lines
809 B
Python

#!/usr/bin/python
class FilterModule(object):
'''Helper filters to make Ansible less unpleasant'''
def filters(self):
return {
'defaultattr': self.defaultattr,
'list2dict': self.list2dict,
'any': self.any,
}
def defaultattr(self, objects, attr, val=None):
'''
Set a default value if the given attribute is not defined for an object.
'''
yield from (obj | { attr: obj.get(attr, val) } for obj in objects)
def list2dict(self, items, key):
'''
Like items2dict but keep entire dictionaries as values.
'''
return {item[key]: item for item in items}
def any(self, items):
'''
Return True if any item in the list is True.
'''
return any(items)