[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
This commit is contained in:
Pavlo Shchelokovskyy 2017-09-20 13:58:18 +00:00
parent 0719dafc2f
commit 6392125993
3 changed files with 19 additions and 23 deletions

View File

@ -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)

View File

@ -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'

View File

@ -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({}) }}"