6

Context: I have a system that has a combination of disks from different storage controllers, so each type of disk has different purpose. I'm new to ansible and learning as I go. Writing a playbook that gets the disk from each type of controller so I can set them up.

Ex. Below is sample output from #'filter=ansible_devices*' ... sdz device is from SATA controller. On my other hosts it might not always be sdz.. so I want to get the device name and store in a variable if in the facts the device has "host": "SATA controller". I'm thinking maybe I need to traverse through ansible_devices dictionaries, find the key that matches ("host": "SATA controller") and then get the parent dict for it which would be the device. Is there a way to do that.. or easier way? :)

"sdz": {
            "holders": [
                "mpathz"
            ],
            "host": "SATA controller: Intel Corporation C610/X99 series chipset 6-Port SATA Controller [AHCI mode] (rev 05)",
            "links": {
                "ids": [
                    "ata-SAMSUNG_MZ7GE960HMHP-00003_S1Y2NYAFC02269",
                    "wwn-0x50025388003aeb2a"
                ],
                "labels": [],
                "masters": [
                    "dm-19"
                ],
                "uuids": []
            },
            "model": "SAMSUNG MZ7GE960",
            "partitions": {},
            "removable": "0",
            "rotational": "0",
            "sas_address": null,
            "sas_device_handle": null,
            "scheduler_mode": "cfq",
            "sectors": "1875385008",
            "sectorsize": "512",
            "serial": "S1Y2NYAFC02269",
            "size": "894.25 GB",
            "support_discard": "512",
            "vendor": "ATA",
            "virtual": 1,
            "wwn": "0x50025388003aeb2a"
comet003
  • 63
  • 1
  • 1
  • 4

2 Answers2

8

This tasks fragment in a playbook should do it, assuming ansible_devices is already set as a variable

 tasks:

    - name: get device name
      set_fact:
        device_name: "{{ item.key }}"
      no_log: True
      with_dict: "{{ ansible_devices }}"
      when: "item.value.host.startswith('SATA')"

    - name: show all values for selected device name
      debug: var=ansible_devices[device_name]

    - name: show only device name
      debug: var=device_name

The set_fact will get your device name. The two debug statements will dump all of the device values and just the device name, respectively.

Rob H
  • 1,676
  • 10
  • 18
  • Thank you so much Rob! So I've been trying for the last day to make the snippet you gave me iterate through rest of devices. It's giving me the right device, that's perfect, but there's a few more devices that match the when statement. I'm thinking it's stopping after it finds the first device that matches the 'SATA'. But when looking through ansible docs, it seems it should work. – comet003 Jan 02 '18 at 16:52
  • You're welcome @CatherineMuturi. I think what is happening is that you're actually getting the LAST match, however the last match may not be what you expect. Try changing the `no_log: True` to `no_log: False` and you'll be able to see the items that are being processed which might show you what is happening. Bottom line I think is that if you have multiple matches you may want to tweak the `when` clause to filter more precisely. – Rob H Jan 02 '18 at 18:40
0

Assuming ansible.builtin.setup has finished, you can get all storage devices using this code:

   - name: initialize empty list for devices
      set_fact:
        storage_devices: []
      no_log: true

    - name: get SATA and NVMe devices
      set_fact:
        storage_devices: "{{ storage_devices + [item.key] }}"
      no_log: true
      with_dict: "{{ ansible_devices }}"
      when: "item.value.host.startswith('SATA controller:') or
             item.value.host.startswith('Non-Volatile memory controller:')"

    - name: show all values for selected devices
      debug: msg="{{ ansible_devices[item] }}"
      loop: "{{ storage_devices }}"

    - name: show devices names
      debug: var=storage_devices

    - name: show first SATA or NVMe device in the list
      debug: var=storage_devices[0]
Zandao
  • 351
  • 3
  • 8