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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With