Last active
August 29, 2015 14:24
-
-
Save andersbc/1b89dc80a7b33dcdab48 to your computer and use it in GitHub Desktop.
Possible ansible bug (2 devel) in custom filters using with_items
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
[defaults] | |
filter_plugins = . |
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
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, | |
} |
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: 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