Skip to content

Instantly share code, notes, and snippets.

@andersbc
Last active August 29, 2015 14:24
Show Gist options
  • Save andersbc/1b89dc80a7b33dcdab48 to your computer and use it in GitHub Desktop.
Save andersbc/1b89dc80a7b33dcdab48 to your computer and use it in GitHub Desktop.
Possible ansible bug (2 devel) in custom filters using with_items
[defaults]
filter_plugins = .
def filter_list(inputlist, attrib, lookuplist):
result = []
for item in inputlist:
if item[attrib] in lookuplist:
result.append(item)
return result
class FilterModule(object):
def filters(self):
return {
"byattr": filter_list,
}
---
- hosts: 127.0.0.1
connection: local
vars:
mylist:
- bar: first
foo: a
- bar: second
foo: b
- bar: third
foo: c
my_filter: ['first', 'second']
# Create a new var with my custom filter applied:
my_filtered_list: "{{ mylist | byattr('bar', my_filter) }}"
tasks:
# works fine - only 'first' and 'second' items are shown
- name: "test 1"
debug: msg="{{ my_filtered_list }}"
# also fine
- name: "test 2"
debug: msg="{{ item }}"
with_items: "{{ mylist }}"
# fails!? with: ...no filter named 'byattr'
- name: "test 3"
debug: msg="{{ item }}"
with_items: "{{ my_filtered_list }}"
# (note the extra ' postfixed to "byattr" in the error message)
# to run (include trailing comma!):
# $ ansible-playbook testplay.yml -i 127.0.0.1,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment