See https://docs.ansible.com/projects/ansible/latest/porting_guides/porting_guide_core_2.19.html#broken-conditionals
28 lines
809 B
Python
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)
|