Last active
November 3, 2017 00:22
-
-
Save farkasmate/09d6bc428c0fa9ba319a65f5d0da5e23 to your computer and use it in GitHub Desktop.
Ansible Filter Plugin: Filter dicts by lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # --- | |
| # - 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