EnigmaticNeo
EnigmaticNeo

Reputation: 45

How to size from specific mount using ansible_mounts?

Looking for way to fetch "just" size_available of a particular mount point on remote host from ansible_hosts fact and set that as fact variable in playbook. Can't really figure that out - please share code snippet.

Upvotes: 0

Views: 2868

Answers (3)

Persaeus
Persaeus

Reputation: 96

For a solution that does not depend on the json_query filter, which requires the jmespath module, you can use the built-in selectattr filter instead:

- debug:
    msg: "{{ (ansible_mounts | selectattr('mount', 'equalto', '/') | list)[0].size_available }}"

Upvotes: 0

adb101
adb101

Reputation: 11

For Ansible 2.8.2 I found that the above solution didn't work and failed with:

templating error while templating string: expected token ',', got 'string'.

The following, however, worked (moving the query text to a variable):

- debug:
    msg: "{{ ansible_mounts|json_query(jmesquery) | first }}"
  vars:
    jmesquery: "[?mount == `/`].size_available"

gives:

"msg": "40047341568"

Upvotes: 1

Vladimir Botka
Vladimir Botka

Reputation: 68034

JSON Query Filter would do the job. For example size_available for mount: /

- debug:
    msg: "{{ ansible_mounts|json_query('[?mount == `/`].size_available') }}"

gives:

"msg": [
    40047341568
]

Upvotes: 0

Related Questions