Skip to content

Instantly share code, notes, and snippets.

@tgerla
Last active August 29, 2015 14:05
Show Gist options
  • Save tgerla/f2ff289a7ea935c48214 to your computer and use it in GitHub Desktop.
Save tgerla/f2ff289a7ea935c48214 to your computer and use it in GitHub Desktop.
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
---
- 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') }}"
@tgerla
Copy link
Author

tgerla commented Aug 28, 2014

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:

ok: [localhost] => {
    "output": {
        "changed": true,
        "failed": true,
        "msg": "One or more items failed.",
        "results": [
            {
                "changed": true,
                "cmd": [
                    "mkdir",
                    "a"
                ],
                "delta": "0:00:00.004365",
                "end": "2014-08-28 15:52:44.798666",
                "invocation": {
                    "module_args": "mkdir a",
                    "module_name": "command"
                },
                "item": "a",
                "rc": 1,
                "start": "2014-08-28 15:52:44.794301",
                "stderr": "mkdir: a: File exists",
                "stdout": "",
                "warnings": [
                    "Consider using file module with state=directory rather than running mkdir"
                ]
            },
            {
                "changed": true,
                "cmd": [
                    "mkdir",
                    "b"
                ],
                "delta": "0:00:00.005014",
                "end": "2014-08-28 15:52:44.899251",
                "invocation": {
                    "module_args": "mkdir b",
                    "module_name": "command"
                },
                "item": "b",
                "rc": 1,
                "start": "2014-08-28 15:52:44.894237",
                "stderr": "mkdir: b: File exists",
                "stdout": "",
                "warnings": [
                    "Consider using file module with state=directory rather than running mkdir"
                ]
            }
        ]
    }
}

Can be transformed into:

ok: [localhost] => {
    "output.results|deeplist(\"stderr\")": [
        "mkdir: a: File exists",
        "mkdir: b: File exists"
    ]
}

To use, put custom_filters.py into your filters_path, specified in ansible.cfg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment