Skip to content

Instantly share code, notes, and snippets.

@halberom
Last active December 14, 2017 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halberom/4f9719a5e14acaae2fc1e940dc9d4cbe to your computer and use it in GitHub Desktop.
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
[defaults]
filter_plugins = ./plugins/filter
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
}
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
---
- 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