<p>Use <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#selecting-json-data-json-queries" rel="nofollow noreferrer">json_query</a>. For example</p>
<pre class="lang-yaml prettyprint-override"><code>    - set_fact:
        my_device: &quot;{{ ansible_mounts|json_query(query) }}&quot;
      vars:
        query: &quot;[?mount=='/'].device &quot;
    - debug:
        var: my_device
</code></pre>
<p>gives</p>
<pre class="lang-yaml prettyprint-override"><code>    my_device:
      - /dev/sdc1
</code></pre>
<p>To get the first element of the list only append the filter <a href="https://jinja.palletsprojects.com/en/latest/templates/#jinja-filters.first" rel="nofollow noreferrer">first</a></p>
<pre class="lang-yaml prettyprint-override"><code>    - set_fact:
        my_device: &quot;{{ ansible_mounts|json_query(query)|first }}&quot;
</code></pre>

CC BY-SA 4.0
2019-10-23T14:16:24.187Z
to get only the value I should use `{{ my_device[0] }}`
sgargel
CC BY-SA 4.0
2019-10-23T14:23:02.247Z
Right. The result is a list because, generally, there might be more hits. It's also possible to append the filter ``{{ ansible_mounts|json_query(query)|first }}`` to get the first element of the list.
Vladimir Botka
CC BY-SA 4.0
2019-10-23T13:08:34.537Z
2021-05-20T12:28:49.570Z
2021-05-20T12:28:49.570Z
Vladimir Botka
<p>Using Ansible facts I can get the list of "ansible_mounts":</p>

<pre>
        "ansible_mounts": [
            {
                "block_available": 423698, 
                "block_size": 4096, 
                "block_total": 507604, 
                "block_used": 83906, 
                "device": "/dev/sdb1", 
                "fstype": "ext4", 
                "inode_available": 130999, 
                "inode_total": 131072, 
                "inode_used": 73, 
                "mount": "/boot", 
                "options": "rw", 
                "size_available": 1735467008, 
                "size_total": 2079145984, 
                "uuid": "b5cfa485-21d2-4e66-b3a3-80f250acf9d9"
            }, 
            {
                "block_available": 42446481, 
                "block_size": 4096, 
                "block_total": 131007993, 
                "block_used": 88561512, 
                "device": "/dev/sdd1", 
                "fstype": "xfs", 
                "inode_available": 524243436, 
                "inode_total": 524287936, 
                "inode_used": 44500, 
                "mount": "/u01", 
                "options": "rw", 
                "size_available": 173860786176, 
                "size_total": 536608739328, 
                "uuid": "f34a9499-ca4e-4db6-801a-16efb72a629d"
            }, 
            {
                "block_available": 3010686, 
                "block_size": 4096, 
                "block_total": 4095466, 
                "block_used": 1084780, 
                "device": "/dev/sdc1", 
                "fstype": "ext4", 
                "inode_available": 984674, 
                "inode_total": 1048576, 
                "inode_used": 63902, 
                "mount": "/", 
                "options": "rw", 
                "size_available": 12331769856, 
                "size_total": 16775028736, 
                "uuid": "829e9504-965b-4660-acc2-12d10d022a3c"
            }
        ], 
</pre>

<p>How I can get the value of <code>device</code> where <code>mount</code> value is "/" ?</p>

<ansible>
How to get the device name of a specified mount point with ansible facts?
sgargel
# How to get the device name of a specified mount point with ansible facts?

(Asked by: username_0 on 2019-10-23)

Using Ansible facts I can get the list of "ansible_mounts":

        "ansible_mounts": [
            {
                "block_available": 423698, 
                "block_size": 4096, 
                "block_total": 507604, 
                "block_used": 83906, 
                "device": "/dev/sdb1", 
                "fstype": "ext4", 
                "inode_available": 130999, 
                "inode_total": 131072, 
                "inode_used": 73, 
                "mount": "/boot", 
                "options": "rw", 
                "size_available": 1735467008, 
                "size_total": 2079145984, 
                "uuid": "b5cfa485-21d2-4e66-b3a3-80f250acf9d9"
            }, 
            {
                "block_available": 42446481, 
                "block_size": 4096, 
                "block_total": 131007993, 
                "block_used": 88561512, 
                "device": "/dev/sdd1", 
                "fstype": "xfs", 
                "inode_available": 524243436, 
                "inode_total": 524287936, 
                "inode_used": 44500, 
                "mount": "/u01", 
                "options": "rw", 
                "size_available": 173860786176, 
                "size_total": 536608739328, 
                "uuid": "f34a9499-ca4e-4db6-801a-16efb72a629d"
            }, 
            {
                "block_available": 3010686, 
                "block_size": 4096, 
                "block_total": 4095466, 
                "block_used": 1084780, 
                "device": "/dev/sdc1", 
                "fstype": "ext4", 
                "inode_available": 984674, 
                "inode_total": 1048576, 
                "inode_used": 63902, 
                "mount": "/", 
                "options": "rw", 
                "size_available": 12331769856, 
                "size_total": 16775028736, 
                "uuid": "829e9504-965b-4660-acc2-12d10d022a3c"
            }
        ], 

How I can get the value of device where mount value is "/" ?

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2019-10-23. Score: 6 | Accepted answer

Use json_query. For example
    - set_fact:
        my_device: "{{ ansible_mounts|json_query(query) }}"
      vars:
        query: "[?mount=='/'].device "
    - debug:
        var: my_device

gives
    my_device:
      - /dev/sdc1

To get the first element of the list only append the filter first
    - set_fact:
        my_device: "{{ ansible_mounts|json_query(query)|first }}"

> **Comments:**
> **username_0**: to get only the value I should use `{{ my_device[0] }}`  
> **username_1**: Right. The result is a list because, generally, there might be more hits. It's also possible to append the filter ``{{ ansible_mounts|json_query(query)|first }}`` to get the first element of the list.  

---

