Last active
December 14, 2017 15:25
-
-
Save halberom/4f9719a5e14acaae2fc1e940dc9d4cbe to your computer and use it in GitHub Desktop.
ansible - NOT RECOMMENDED - example of incredibly nasty/unsafe custom filter to provide list comprehension
This file contains 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
[defaults] | |
filter_plugins = ./plugins/filter |
This file contains 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
from __future__ import (absolute_import, division, print_function) | |
__metaclass__ = type | |
from ansible import errors | |
def comp(value, declaration, condition=None): | |
""" | |
support list comprehension | |
[x for x in y if something] | |
""" | |
# yuck - using eval() - bad! | |
if condition: | |
result = [eval(declaration) for x in value if eval(condition)] | |
else: | |
result = [eval(declaration) for x in value] | |
return result | |
class FilterModule(object): | |
def filters(self): | |
return { | |
'comp': comp | |
} |
This file contains 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
PLAY [localhost] *************************************************************************************************************************************************************************************************************************************************************** | |
TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************* | |
ok: [localhost] => { | |
"msg": [ | |
"c", | |
"d" | |
] | |
} | |
PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************* | |
localhost : ok=1 changed=0 unreachable=0 failed=0 | |
PLAY [localhost] *************************************************************************************************************************************************************************************************************************************************************** | |
TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************* | |
ok: [localhost] => { | |
"msg": [ | |
"bob" | |
] | |
} | |
PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************* | |
localhost : ok=1 changed=0 unreachable=0 failed=0 |
This file contains 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: False | |
connection: local | |
vars: | |
mylistoflists: [['a', 'b'],['c', 1],['d', 1]] | |
tasks: | |
- debug: | |
msg: "{{ mylistoflists|comp('x[0]', 'x[1] == 1')|list }}" | |
- hosts: localhost | |
gather_facts: False | |
connection: local | |
vars: | |
mylistofdicts: | |
- { foo: bob, bar: xyz, type: 1 } | |
- { foo: fred, bar: xyz, type: 2 } | |
tasks: | |
- debug: | |
msg: "{{ mylistofdicts|comp(declaration, condition) }}" | |
vars: | |
declaration: "x['foo']" | |
condition: "x['type'] == 1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment