From 6392125993c64c61402bdf618c83f978b52de7d0 Mon Sep 17 00:00:00 2001 From: Pavlo Shchelokovskyy Date: Wed, 20 Sep 2017 13:58:18 +0000 Subject: [PATCH] [ansible] fix wwn facts and root device hints Apparently only checking that pyudev is importable is not enough, as it can fail later if udev itself is not available on the system Also bring the (empty) default between these two to be empty dict, and fix logging invocations in WWN fact collecting module. Change-Id: I137990280e90c9ca2687b38d98f6c5343e26e8d0 --- .../ansible/playbooks/library/facts_wwn.py | 36 +++++++++---------- .../ansible/playbooks/library/root_hints.py | 3 +- .../roles/discover/tasks/roothints.yaml | 3 +- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/ironic_staging_drivers/ansible/playbooks/library/facts_wwn.py b/ironic_staging_drivers/ansible/playbooks/library/facts_wwn.py index 0db549d..71497ce 100644 --- a/ironic_staging_drivers/ansible/playbooks/library/facts_wwn.py +++ b/ironic_staging_drivers/ansible/playbooks/library/facts_wwn.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # -*- coding: utf-8 -*- # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -13,36 +12,33 @@ # License for the specific language governing permissions and limitations # under the License. -try: - import pyudev - HAS_PYUDEV = True -except ImportError: - HAS_PYUDEV = False - - COLLECT_INFO = (('wwn', 'WWN'), ('serial', 'SERIAL_SHORT'), ('wwn_with_extension', 'WWN_WITH_EXTENSION'), ('wwn_vendor_extension', 'WWN_VENDOR_EXTENSION')) -def get_devices_wwn(devices): - - if not HAS_PYUDEV: - LOG.warning('Can not collect "wwn", "wwn_with_extension", ' - '"wwn_vendor_extension" and "serial" when using ' - 'root device hints because there\'s no UDEV python ' - 'binds installed') - return +# TODO(pas-ha) replace module.log with module.warn +# after we require Ansible >= 2.3 +def get_devices_wwn(devices, module): + try: + import pyudev + # NOTE(pas-ha) creating context might fail if udev is missing + context = pyudev.Context() + except ImportError: + module.log('Can not collect "wwn", "wwn_with_extension", ' + '"wwn_vendor_extension" and "serial" when using ' + 'root device hints because there\'s no UDEV python ' + 'binds installed') + return {} dev_dict = {} - context = pyudev.Context() for device in devices: name = '/dev/' + device try: udev = pyudev.Device.from_device_file(context, name) except (ValueError, EnvironmentError, pyudev.DeviceNotFoundError) as e: - LOG.warning('Device %(dev)s is inaccessible, skipping... ' - 'Error: %(error)s', {'dev': name, 'error': e}) + module.log('Device %(dev)s is inaccessible, skipping... ' + 'Error: %(error)s', {'dev': name, 'error': e}) continue dev_dict[device] = {} @@ -61,7 +57,7 @@ def main(): ) devices = module.params['devices'] - data = get_devices_wwn(devices) + data = get_devices_wwn(devices, module) module.exit_json(**data) diff --git a/ironic_staging_drivers/ansible/playbooks/library/root_hints.py b/ironic_staging_drivers/ansible/playbooks/library/root_hints.py index 3fc6569..32473eb 100644 --- a/ironic_staging_drivers/ansible/playbooks/library/root_hints.py +++ b/ironic_staging_drivers/ansible/playbooks/library/root_hints.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # -*- coding: utf-8 -*- # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -74,7 +73,7 @@ def main(): devices = module.params['ansible_devices'] devices_wwn = module.params['ansible_devices_wwn'] - if devices_wwn is None: + if not devices_wwn: extra = set(hints) & EXTRA_PARAMS if extra: module.fail_json(msg='Extra hints (supported by additional ansible' diff --git a/ironic_staging_drivers/ansible/playbooks/roles/discover/tasks/roothints.yaml b/ironic_staging_drivers/ansible/playbooks/roles/discover/tasks/roothints.yaml index 03ed582..488a218 100644 --- a/ironic_staging_drivers/ansible/playbooks/roles/discover/tasks/roothints.yaml +++ b/ironic_staging_drivers/ansible/playbooks/roles/discover/tasks/roothints.yaml @@ -1,8 +1,9 @@ - name: get devices wwn facts facts_wwn: devices: "{{ ansible_devices.keys() }}" + - name: calculate root hint root_hints: root_device_hints: "{{ ironic.root_device_hints }}" ansible_devices: "{{ ansible_devices }}" - ansible_devices_wwn: "{{ devices_wwn | default(None) }}" + ansible_devices_wwn: "{{ devices_wwn | default({}) }}"