Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to get the list value returned by the json query from the ansible facts

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{{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"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading