Skip to content

Instantly share code, notes, and snippets.

@farkasmate
Last active November 3, 2017 00:22
Show Gist options
  • Select an option

  • Save farkasmate/09d6bc428c0fa9ba319a65f5d0da5e23 to your computer and use it in GitHub Desktop.

Select an option

Save farkasmate/09d6bc428c0fa9ba319a65f5d0da5e23 to your computer and use it in GitHub Desktop.
Ansible Filter Plugin: Filter dicts by lambda
# ---
# - hosts: 'localhost'
# gather_facts: 'no'
# tasks:
# - set_fact:
# ports:
# Ethernet0:
# ip: '10.0.0.1'
# description: 'csw11'
# Ethernet1:
# ip: '10.0.0.21'
# description: ''
# lambda_string: "lambda x: x[1]['description'] == ''" # x is a (key, value) tuple of the input dict
# - debug:
# msg: "{{ ports | filter_dict(lambda_string) | list }}"
def filter_dict(dictionary, lambda_string):
return dict(filter(eval(lambda_string), dictionary.items()))
class FilterModule(object):
def filters(self):
return {
'filter_dict' : filter_dict
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment