diff --git a/changelogs/fragments/freebsd_facts_refactor.yml b/changelogs/fragments/freebsd_facts_refactor.yml new file mode 100644 index 00000000000..f0f01354f76 --- /dev/null +++ b/changelogs/fragments/freebsd_facts_refactor.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - freebsd - refactor dmidecode fact gathering code for simplicity. diff --git a/lib/ansible/module_utils/facts/hardware/freebsd.py b/lib/ansible/module_utils/facts/hardware/freebsd.py index a0d493f10c8..d7a3800988f 100644 --- a/lib/ansible/module_utils/facts/hardware/freebsd.py +++ b/lib/ansible/module_utils/facts/hardware/freebsd.py @@ -253,18 +253,22 @@ class FreeBSDHardware(Hardware): 'product_version': 'system-version', 'system_vendor': 'system-manufacturer', } + if dmi_bin is None: + dmi_facts = dict.fromkeys( + DMI_DICT.keys(), + 'NA' + ) + return dmi_facts + for (k, v) in DMI_DICT.items(): - if dmi_bin is not None: - (rc, out, err) = self.module.run_command('%s -s %s' % (dmi_bin, v)) - if rc == 0: - # Strip out commented lines (specific dmidecode output) - # FIXME: why add the fact and then test if it is json? - dmi_facts[k] = ''.join([line for line in out.splitlines() if not line.startswith('#')]) - try: - json.dumps(dmi_facts[k]) - except UnicodeDecodeError: - dmi_facts[k] = 'NA' - else: + (rc, out, err) = self.module.run_command('%s -s %s' % (dmi_bin, v)) + if rc == 0: + # Strip out commented lines (specific dmidecode output) + # FIXME: why add the fact and then test if it is json? + dmi_facts[k] = ''.join([line for line in out.splitlines() if not line.startswith('#')]) + try: + json.dumps(dmi_facts[k]) + except UnicodeDecodeError: dmi_facts[k] = 'NA' else: dmi_facts[k] = 'NA' diff --git a/lib/ansible/module_utils/facts/hardware/linux.py b/lib/ansible/module_utils/facts/hardware/linux.py index 1c535e22318..a499cdee5c4 100644 --- a/lib/ansible/module_utils/facts/hardware/linux.py +++ b/lib/ansible/module_utils/facts/hardware/linux.py @@ -393,20 +393,24 @@ class LinuxHardware(Hardware): 'product_version': 'system-version', 'system_vendor': 'system-manufacturer', } + if dmi_bin is None: + dmi_facts = dict.fromkeys( + DMI_DICT.keys(), + 'NA' + ) + return dmi_facts + for (k, v) in DMI_DICT.items(): - if dmi_bin is not None: - (rc, out, err) = self.module.run_command('%s -s %s' % (dmi_bin, v)) - if rc == 0: - # Strip out commented lines (specific dmidecode output) - thisvalue = ''.join([line for line in out.splitlines() if not line.startswith('#')]) - try: - json.dumps(thisvalue) - except UnicodeDecodeError: - thisvalue = "NA" + (rc, out, err) = self.module.run_command('%s -s %s' % (dmi_bin, v)) + if rc == 0: + # Strip out commented lines (specific dmidecode output) + thisvalue = ''.join([line for line in out.splitlines() if not line.startswith('#')]) + try: + json.dumps(thisvalue) + except UnicodeDecodeError: + thisvalue = "NA" - dmi_facts[k] = thisvalue - else: - dmi_facts[k] = 'NA' + dmi_facts[k] = thisvalue else: dmi_facts[k] = 'NA'