Retrieving items from a dictionary

24 views
Skip to first unread message

lift...@gmail.com

unread,
Sep 27, 2023, 1:11:07 PM9/27/23
to Ansible Project
I am trying to look at our server  mounts and only show those that have a specific mount available.  The playbook is as follows:

---
- hosts: all_hosts
  become: true
  become_method: sudo
  gather_facts: true

  tasks:


  - name: Get mount points
    ansible.builtin.set_fact:
      device_names: "{{ ansible_mounts | selectattr('device', 'contains', 'nas-server.example.com') | list }}"

  - name: List servers with those mount points
    ansible.builtin.debug:
      msg: "{{ device_names }}"

I have 2 small issues with the output:
1) The device_names shows everything about that mount and I only want to show the device attribute;
2) Hosts that do not have the mount present show up in my debug list with an empty string.  I'd like to not show them at all if possible.

ok: [server1.example.com] => {
    "msg": [
        {
            "block_available": 26201799211,
            "block_size": 4096,
            "block_total": 36234081792,
            "block_used": 10032282581,
            "device": "nas-server.example.com:/projects",
            "fstype": "fuse.glusterfs",
            "inode_available": 14493376449,
            "inode_total": 14495510592,
            "inode_used": 2134143,
            "mount": "/projects",
            "options": "rw,nodev,relatime,user_id=0,group_id=0,allow_other,max_read=131072",
            "size_available": 107322569568256,
            "size_total": 148414799020032,
            "uuid": "N/A"
        }
    ]
}
ok: [server2.example.com] => {
    "msg": []
}

Thanks for any help,
Harry

Vladimir Botka

unread,
Sep 27, 2023, 3:33:10 PM9/27/23
to lift...@gmail.com, ansible...@googlegroups.com
Map the attribute

dn: "{{ ansible_mounts|
selectattr('device', 'contains', 'nas-server.example.com')|
map(attribute='device') }}"

If you want to skip empty lists test the length

- debug:
var: dn
when: dn|length > 0

--
Vladimir Botka

harry devine

unread,
Sep 28, 2023, 8:07:28 AM9/28/23
to Vladimir Botka, ansible...@googlegroups.com
Thank you Vladimir!  I had already gotten the selectattr piece working but the length check was the missing piece.  All good now.

Thanks,
Harry 
Reply all
Reply to author
Forward
0 new messages