Reputation: 16753
Ansible v2.2.1.0
I have task that collects information over items, and I setup a register for the task. For example, I use jq to parse a JSON file,
hello.json
----------
{
"name" : "hello file",
"english" : "hello",
"spanish" : "hola",
"german" : "wie gehts"
}
- name: parse the hello.json file
shell: |
jq -r '.{{ item }}' < hello.json
register: hellos
with_items:
- english
- spanish
- german
- debug: var=hellos
The debug shows
ok: [localhost] => {
"hellos": {
"changed": true,
"msg": "All items completed",
"results": [
{
# snipped
"item": "english",
"stdout" : "hello",
# snipped
},
{
# snipped
"item": "spanish",
"stdout" : "hola",
# snipped
},
{
# snipped
"item": "german",
"stdout" : "wie gehts",
# snipped
}
]
}
}
Now if I want to get the stdout value of the hellos register, I tried this
- name: Display hello messages
debug: msg="{{ hellos.results | selectattr("item","equalto",item) | map(attribute="stdout") | first }} worlld"
with_items:
- english
- spanish
- german
I get
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TemplateRuntimeError: no test named 'equalto'
fatal: [localhost]: FAILED! => {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}
I'm basically parsing the hellos register for the "item" and getting its "stdout" attribute i the second debug task. Where's my error?
Upvotes: 11
Views: 17190
Reputation: 2452
Ansible in version 2.3 and less has no test named equalto
, so you can create one custom test plugin name equal_to
. Simply create one file in the test_plugins directory.
#! /usr/bin/python
# vi ./test_plugins/anything.py
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
def equal_to(value, other):
return value == other
class TestModule(object):
def tests(self):
return {
'equal_to': equal_to
}
Happy developments...
Upvotes: 0
Reputation: 68289
You do some really strange stuff here. I'm sure your original task can be solved much easier.
But to answer your question "Why equalto
filter not found?": update Jinja2.
Check with pip list | grep Jinja2
.
equalto
is introduced in ver. 2.8.
Upvotes: 11