0

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.

Fluki
  • 3
  • 1

1 Answers1

1

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 }}"
Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39