Looping facter_disks in ansible

Viewed 145

I am trying to loop thru facter_disks to find disk with a particular size. In the below code would like to get disk sdb with size of 30 . Any help is greatly appreciated


---
- name: sample Playbook
  hosts: localhost
  gather_facts: false
  become: yes
  vars:
    disks:
      - sda:
          model: SYMMETRIX
          size: 55.00 GiB
          size_bytes: 59057111040
          vendor: EMC
        sdb:
          model: SYMMETRIX
          size: 35.00 GiB
          size_bytes: 37581619200
          vendor: EMC
        sdc:
          model: SYMMETRIX
          size: 20.00 GiB
          size_bytes: 21475491840
          vendor: EMC


  tasks:

    - debug:
        msg:
#         - "{{ disks[0]['sda']['size'] }}"
          - "{{ item['sda']['size'] }}"
      with_items: " {{disks}} "

1 Answers

disks var is a list then you need to use "-" before sda, sdb, sdc. In this case if you want to use disks as a list of disks then you can modify and set a name property as I write below

---
- name: sample Playbook
  hosts: localhost
  gather_facts: false
  become: yes
  vars:
    disks:
    - name: sda
      model: SYMMETRIX
      size: 55.00 GiB
      size_bytes: 59057111040
      vendor: EMC
    - name: sdb
      model: SYMMETRIX
      size: 35.00 GiB
      size_bytes: 37581619200
      vendor: EMC
    - name: sdc
      model: SYMMETRIX
      size: 20.00 GiB
      size_bytes: 21475491840
      vendor: EMC

  tasks:
    - debug:
        msg:
        - "{{ item.name }}"
        - "{{ item.model }}"
        - "{{ item.size }}"
        - "{{ item.size_bytes }}"
        - "{{ item.vendor }}"
      with_items: " {{disks}}"
      when: item.size == "35.00 GiB"

Related