Ansible: Using ansible facts, how can I get a disk device based on the storage controller value and then set the device as a variable
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.
Admin
Updated on July 19, 2022Comments
-
Admin almost 2 years
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"