I am trying to get the size_available value for the /home filesystem from the ansible facts.
I am using the following code after setting gather_facts: True
{{ansible_facts['mounts']|json_query('[?mount==`/home`].size_available')}}
This way I get something like this [34545646] with msg: from the debug module.
I need to compare this value to a static one and continue or not the playbook but when I try:
{{ansible_facts['mounts']|json_query('[?mount==`/home`].size_available')[0]}}
I get:
"msg": "template error while templating string: expected token 'end of print statement', got '['. String: > {{ansible_facts['mounts']|json_query('[?mount==`/home`].size_available')[0]}}
Even if the type_debug shows me the result should be indeed a list that should be accessible by the [0] extension.
>Solution :
You need to parenthesize the initial expression before attempting to index the result, like this:
{{
(
ansible_facts['mounts'] |
json_query('[?mount==`/home`].size_available')
)[0]
}}
E.g., if I run this playbook on my system:
- hosts: localhost
gather_facts: true
tasks:
- debug:
msg: >-
{{
(
ansible_facts['mounts'] |
json_query('[?mount==`/home`].size_available')
)[0]
}}
I get this output for the debug task:
TASK [debug] ********************************************************************************************
ok: [localhost] => {
"msg": "402658955264"
}