3

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 "/" ?

6

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 }}"
2
  • to get only the value I should use {{ my_device[0] }}
    – sgargel
    Oct 23 '19 at 14:16
  • 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. Oct 23 '19 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.