Last active
August 29, 2015 14:05
-
-
Save tgerla/f2ff289a7ea935c48214 to your computer and use it in GitHub Desktop.
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
from jinja2 import contextfilter | |
class FilterModule(object): | |
''' Extra filters ''' | |
def filters(self): | |
return { | |
'deeplist': self.deeplist, | |
} | |
def deeplist(self, obj, key): | |
r = [] | |
for x in obj: | |
if key in x: | |
r.append(x[key]) | |
return r |
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 | |
connection: local | |
tasks: | |
- name: test | |
command: mkdir {{ item }} | |
with_items: | |
- a | |
- b | |
register: output | |
ignore_errors: true | |
- debug: var=output | |
- debug: var=output.results|deeplist("stderr") | |
- name: output | |
debug: msg="Error from mkdir" | |
with_items: "{{ output.results|deeplist('stderr') }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This Ansible filter will take a list of dicts and transform it into a flat list based on one key that you specify. This may be particularly useful dealing with the results from a combined register+with_items task.
For example, this output:
Can be transformed into:
To use, put custom_filters.py into your filters_path, specified in ansible.cfg.