diff --git a/changelogs/fragments/59877-fix-keyerror-in-redfish-getlogs.yaml b/changelogs/fragments/59877-fix-keyerror-in-redfish-getlogs.yaml new file mode 100644 index 0000000000..78b026d2b1 --- /dev/null +++ b/changelogs/fragments/59877-fix-keyerror-in-redfish-getlogs.yaml @@ -0,0 +1,3 @@ +--- +bugfixes: +- redfish_facts - fix KeyError exceptions in GetLogs (https://github.com/ansible/ansible/issues/59797) diff --git a/lib/ansible/module_utils/redfish_utils.py b/lib/ansible/module_utils/redfish_utils.py index c199bac9ec..cf80172b77 100644 --- a/lib/ansible/module_utils/redfish_utils.py +++ b/lib/ansible/module_utils/redfish_utils.py @@ -269,6 +269,8 @@ class RedfishUtils(object): def get_logs(self): log_svcs_uri_list = [] list_of_logs = [] + properties = ['Severity', 'Created', 'EntryType', 'OemRecordFormat', + 'Message', 'MessageId', 'MessageArgs'] # Find LogService response = self.get_request(self.root_uri + self.manager_uri) @@ -284,12 +286,13 @@ class RedfishUtils(object): if response['ret'] is False: return response data = response['data'] - for log_svcs_entry in data[u'Members']: + for log_svcs_entry in data.get('Members', []): response = self.get_request(self.root_uri + log_svcs_entry[u'@odata.id']) if response['ret'] is False: return response _data = response['data'] - log_svcs_uri_list.append(_data['Entries'][u'@odata.id']) + if 'Entries' in _data: + log_svcs_uri_list.append(_data['Entries'][u'@odata.id']) # For each entry in LogServices, get log name and all log entries for log_svcs_uri in log_svcs_uri_list: @@ -299,15 +302,16 @@ class RedfishUtils(object): if response['ret'] is False: return response data = response['data'] - logs['Description'] = data['Description'] + logs['Description'] = data.get('Description', + 'Collection of log entries') # Get all log entries for each type of log found - for logEntry in data[u'Members']: - # I only extract some fields - Are these entry names standard? - list_of_log_entries.append(dict( - Name=logEntry[u'Name'], - Created=logEntry[u'Created'], - Message=logEntry[u'Message'], - Severity=logEntry[u'Severity'])) + for logEntry in data.get('Members', []): + entry = {} + for prop in properties: + if prop in logEntry: + entry[prop] = logEntry.get(prop) + if entry: + list_of_log_entries.append(entry) log_name = log_svcs_uri.split('/')[-1] logs[log_name] = list_of_log_entries list_of_logs.append(logs)