Score:0

Error tryng to retreive mount points and device of an host with ansible

ml flag

Hi i'm trying to retreive some infos about hosts with ansible about mount points and device attached and a got an error . about the list object..

- name : Inventory
  hosts: localhost
  tasks:
    
    - name: display infos from host
      ansible.builtin.debug:
        var: ansible_facts

    - name: store disks infos
      ansible.builtin.lineinfile:
        path: "tmp/inventory.log"
        line: "{{ item.device}}"
        create: true
      loop:
        - "{{ ansible_facts.mounts }}"

The error is :

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'device'. 'list object' has no attribute 'device'\n\nThe error appears to be in '/home/user/workspace/.../playbook-inventory.yml': line 31, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: store disks infos\n      ^ here\n"}

But when i look to what is inside :

changed: [localhost] => (item=[{'mount': '/', 'device': '/dev/sdc', 'fstype': 'ext4', 'options': 'rw,relatime,discard,errors=remount-ro,data=ordered', 'size_total': 1081101176832, 'size_available': 1017088557056, 'block_size': 4096, 'block_total': 263940717, 'block_available': 248312636, 'block_used': 15628081, 'inode_total': 67108864, 'inode_available': 66837030, 'inode_used': 271834, 'uuid': '495dd145-115b-40d8-8fe3-e7c50afc22b4'}, {'mount': '/mnt/wslg/distro', 'device': '/dev/sdc', 'fstype': 'ext4', 'options': 'ro,relatime,discard,errors=remount-ro,data=ordered', 'size_total': 1081101176832, 'size_available': 1017088557056, 'block_size': 4096, 'block_total': 263940717, 'block_available': 248312636, 'block_used': 15628081, 'inode_total': 67108864, 'inode_available': 66837030, 'inode_used': 271834, 'uuid': '495dd145-115b-40d8-8fe3-e7c50afc22b4'}, {'mount': '/snap', 'device': '/dev/sdc', 'fstype': 'ext4', 'options': 'rw,relatime,discard,errors=remount-ro,data=ordered,bind', 'size_total': 1081101176832, 'size_available': 1017088557056, 'block_size': 4096, 'block_total': 263940717, 'block_available': 248312636, 'block_used': 15628081, 'inode_total': 67108864, 'inode_available': 66837030, 'inode_used': 271834, 'uuid': '495dd145-115b-40d8-8fe3-e7c50afc22b4'}, {'mount': '/var/lib/docker', 'device': '/dev/sdc', 'fstype': 'ext4', 'options': 'rw,relatime,discard,errors=remount-ro,data=ordered,bind', 'size_total': 1081101176832, 'size_available': 1017088557056, 'block_size': 4096, 'block_total': 263940717, 'block_available': 248312636, 'block_used': 15628081, 'inode_total': 67108864, 'inode_available': 66837030, 'inode_used': 271834, 'uuid': '495dd145-115b-40d8-8fe3-e7c50afc22b4'}])

Am i doing something wrong ? do you have an idea of what is going on ?

Thank you for your help and your time guys.

Score:1
in flag

You need to use {{ item['key'] }} to get the dictionary value. Like so:

    - name: store disks infos
      ansible.builtin.lineinfile:
        path: "tmp/inventory.log"
        line: "{{ item['device'] }}"
        create: true
      loop: "{{ ansible_facts.mounts }}"
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.