diff --git a/changelogs/fragments/sysctl_fact_fix.yml b/changelogs/fragments/sysctl_fact_fix.yml index 18933402158..55f51b91428 100644 --- a/changelogs/fragments/sysctl_fact_fix.yml +++ b/changelogs/fragments/sysctl_fact_fix.yml @@ -1,2 +1,3 @@ +--- bugfixes: - - setup/gather_facts will now warn and skip missing ``sysctl`` instead of being a fatal error. + - setup/gather_facts will skip missing ``sysctl`` instead of being a fatal error (https://github.com/ansible/ansible/pull/81297). diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index bed2aaef260..154b081c78a 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -1348,14 +1348,13 @@ class AnsibleModule(object): # and we don't want to break modules unnecessarily return None - def get_bin_path(self, arg, required=False, opt_dirs=None, warning=None): + def get_bin_path(self, arg, required=False, opt_dirs=None): ''' Find system executable in PATH. :param arg: The executable to find. :param required: if the executable is not found and required is ``True``, fail_json :param opt_dirs: optional list of directories to search in addition to ``PATH`` - :param warning: optional message when arg not found, only works when required=False :returns: if found return full path; otherwise return original arg, unless 'warning' then return None :raises: Sysexit: if arg is not found and required=True (via fail_json) ''' @@ -1366,8 +1365,6 @@ class AnsibleModule(object): except ValueError as e: if required: self.fail_json(msg=to_text(e)) - elif warning is not None: - self.warn("Unable to find %s, %s" % (arg, warning)) return bin_path diff --git a/lib/ansible/module_utils/facts/hardware/aix.py b/lib/ansible/module_utils/facts/hardware/aix.py index 6afd36d86f1..c2a074bf8ea 100644 --- a/lib/ansible/module_utils/facts/hardware/aix.py +++ b/lib/ansible/module_utils/facts/hardware/aix.py @@ -129,7 +129,7 @@ class AIXHardware(Hardware): rc, out, err = self.module.run_command("/usr/sbin/lsattr -El sys0 -a fwversion") data = out.split() dmi_facts['firmware_version'] = data[1].strip('IBM,') - lsconf_path = self.module.get_bin_path("lsconf", warning="dmi facts skipped") + lsconf_path = self.module.get_bin_path("lsconf") if lsconf_path: rc, out, err = self.module.run_command(lsconf_path) if rc == 0 and out: @@ -160,9 +160,8 @@ class AIXHardware(Hardware): """ vgs_facts = {} - warn = "vgs facts skipped" - lsvg_path = self.module.get_bin_path("lsvg", warning=warn) - xargs_path = self.module.get_bin_path("xargs", warning=warn) + lsvg_path = self.module.get_bin_path("lsvg") + xargs_path = self.module.get_bin_path("xargs") cmd = "%s -o | %s %s -p" % (lsvg_path, xargs_path, lsvg_path) if lsvg_path and xargs_path: rc, out, err = self.module.run_command(cmd, use_unsafe_shell=True) @@ -195,7 +194,7 @@ class AIXHardware(Hardware): # AIX does not have mtab but mount command is only source of info (or to use # api calls to get same info) - mount_path = self.module.get_bin_path('mount', warning="skipping mount facts") + mount_path = self.module.get_bin_path('mount') if mount_path: rc, mount_out, err = self.module.run_command(mount_path) if mount_out: @@ -234,9 +233,8 @@ class AIXHardware(Hardware): device_facts = {} device_facts['devices'] = {} - warn = 'device facts are skipped' - lsdev_cmd = self.module.get_bin_path('lsdev', warning=warn) - lsattr_cmd = self.module.get_bin_path('lsattr', warning=warn) + lsdev_cmd = self.module.get_bin_path('lsdev') + lsattr_cmd = self.module.get_bin_path('lsattr') if lsdev_cmd and lsattr_cmd: rc, out_lsdev, err = self.module.run_command(lsdev_cmd) diff --git a/lib/ansible/module_utils/facts/hardware/darwin.py b/lib/ansible/module_utils/facts/hardware/darwin.py index fc3c877805e..ac159d5fd2b 100644 --- a/lib/ansible/module_utils/facts/hardware/darwin.py +++ b/lib/ansible/module_utils/facts/hardware/darwin.py @@ -95,10 +95,7 @@ class DarwinHardware(Hardware): total_used = 0 page_size = 4096 - vm_stat_command = self.module.get_bin_path( - 'vm_stat', - warning='falling back to sysctl for memtotal_mb, default to 0 for memfree_mb' - ) + vm_stat_command = self.module.get_bin_path('vm_stat') if vm_stat_command is None: return memory_facts @@ -136,7 +133,7 @@ class DarwinHardware(Hardware): # On Darwin, the default format is annoying to parse. # Use -b to get the raw value and decode it. - sysctl_cmd = self.module.get_bin_path('sysctl', warning='skipping uptime facts') + sysctl_cmd = self.module.get_bin_path('sysctl') if not sysctl_cmd: return {} diff --git a/lib/ansible/module_utils/facts/hardware/linux.py b/lib/ansible/module_utils/facts/hardware/linux.py index af1fd426a52..c77c5937f23 100644 --- a/lib/ansible/module_utils/facts/hardware/linux.py +++ b/lib/ansible/module_utils/facts/hardware/linux.py @@ -304,7 +304,7 @@ class LinuxHardware(Hardware): ) except AttributeError: # In Python < 3.3, os.sched_getaffinity() is not available - nproc_cmd = self.module.get_bin_path('nproc', warning="skipping processor_nproc") + nproc_cmd = self.module.get_bin_path('nproc') if nproc_cmd is not None: rc, out, _err = self.module.run_command(nproc_cmd) if rc == 0: @@ -391,10 +391,7 @@ class LinuxHardware(Hardware): 'product_version': 'system-version', 'system_vendor': 'system-manufacturer', } - dmi_bin = self.module.get_bin_path( - 'dmidecode', - warning="skipping dmi facts" - ) + dmi_bin = self.module.get_bin_path('dmidecode') if dmi_bin is None: dmi_facts = dict.fromkeys( DMI_DICT.keys(), @@ -865,10 +862,7 @@ class LinuxHardware(Hardware): """ Get LVM Facts if running as root and lvm utils are available """ lvm_facts = {'lvm': 'N/A'} - vgs_cmd = self.module.get_bin_path( - 'vgs', - warning="skipping LVM facts" - ) + vgs_cmd = self.module.get_bin_path('vgs') if vgs_cmd is None: return lvm_facts diff --git a/lib/ansible/module_utils/facts/hardware/netbsd.py b/lib/ansible/module_utils/facts/hardware/netbsd.py index bcde75d3bcf..69ac583df64 100644 --- a/lib/ansible/module_utils/facts/hardware/netbsd.py +++ b/lib/ansible/module_utils/facts/hardware/netbsd.py @@ -161,10 +161,7 @@ class NetBSDHardware(Hardware): def get_uptime_facts(self): # On NetBSD, we need to call sysctl with -n to get this value as an int. - sysctl_cmd = self.module.get_bin_path( - 'sysctl', - warning="skipping uptime facts" - ) + sysctl_cmd = self.module.get_bin_path('sysctl') if sysctl_cmd is None: return {} diff --git a/lib/ansible/module_utils/facts/hardware/openbsd.py b/lib/ansible/module_utils/facts/hardware/openbsd.py index 909ff444dee..f6765422536 100644 --- a/lib/ansible/module_utils/facts/hardware/openbsd.py +++ b/lib/ansible/module_utils/facts/hardware/openbsd.py @@ -112,10 +112,7 @@ class OpenBSDHardware(Hardware): def get_uptime_facts(self): # On openbsd, we need to call it with -n to get this value as an int. - sysctl_cmd = self.module.get_bin_path( - 'sysctl', - warning="skipping uptime facts" - ) + sysctl_cmd = self.module.get_bin_path('sysctl') if sysctl_cmd is None: return {} diff --git a/lib/ansible/module_utils/facts/hardware/sunos.py b/lib/ansible/module_utils/facts/hardware/sunos.py index d67dc4dc2ff..134e59a8c2c 100644 --- a/lib/ansible/module_utils/facts/hardware/sunos.py +++ b/lib/ansible/module_utils/facts/hardware/sunos.py @@ -174,8 +174,7 @@ class SunOSHardware(Hardware): prtdiag_path = self.module.get_bin_path( "prtdiag", - opt_dirs=[platform_sbin], - warning="skipping dmi facts" + opt_dirs=[platform_sbin] ) if prtdiag_path is None: return dmi_facts diff --git a/lib/ansible/module_utils/facts/network/aix.py b/lib/ansible/module_utils/facts/network/aix.py index 9c95c5e906a..17516d927d8 100644 --- a/lib/ansible/module_utils/facts/network/aix.py +++ b/lib/ansible/module_utils/facts/network/aix.py @@ -31,10 +31,7 @@ class AIXNetwork(GenericBsdIfconfigNetwork): def get_default_interfaces(self, route_path): interface = dict(v4={}, v6={}) - netstat_path = self.module.get_bin_path( - 'netstat', - warning="skipping default interface facts" - ) + netstat_path = self.module.get_bin_path('netstat') if netstat_path is None: return interface['v4'], interface['v6'] diff --git a/lib/ansible/module_utils/facts/network/fc_wwn.py b/lib/ansible/module_utils/facts/network/fc_wwn.py index 24ca26a26db..fb846cc08a8 100644 --- a/lib/ansible/module_utils/facts/network/fc_wwn.py +++ b/lib/ansible/module_utils/facts/network/fc_wwn.py @@ -47,10 +47,7 @@ class FcWwnInitiatorFactCollector(BaseFactCollector): elif sys.platform.startswith('sunos'): # on solaris 10 or solaris 11 should use `fcinfo hba-port` # TBD (not implemented): on solaris 9 use `prtconf -pv` - cmd = module.get_bin_path( - 'fcinfo', - warning="skipping fibre wwn initiator facts" - ) + cmd = module.get_bin_path('fcinfo') if cmd: cmd = cmd + " hba-port" rc, fcinfo_out, err = module.run_command(cmd) @@ -62,14 +59,8 @@ class FcWwnInitiatorFactCollector(BaseFactCollector): data = line.split(' ') fc_facts['fibre_channel_wwn'].append(data[-1].rstrip()) elif sys.platform.startswith('aix'): - cmd = module.get_bin_path( - 'lsdev', - warning="skipping fibre wwn initiator facts" - ) - lscfg_cmd = module.get_bin_path( - 'lscfg', - warning="skipping fibre wwn initiator facts" - ) + cmd = module.get_bin_path('lsdev') + lscfg_cmd = module.get_bin_path('lscfg') if cmd and lscfg_cmd: # get list of available fibre-channel devices (fcs) cmd = cmd + " -Cc adapter -l fcs*" @@ -90,14 +81,10 @@ class FcWwnInitiatorFactCollector(BaseFactCollector): data = line.split('.') fc_facts['fibre_channel_wwn'].append(data[-1].rstrip()) elif sys.platform.startswith('hp-ux'): - cmd = module.get_bin_path( - 'ioscan', - warning="skipping fibre wwn initiator facts" - ) + cmd = module.get_bin_path('ioscan') fcmsu_cmd = module.get_bin_path( 'fcmsutil', opt_dirs=['/opt/fcms/bin'], - warning="skipping fibre wwn initiator facts" ) # go ahead if we have both commands available if cmd and fcmsu_cmd: diff --git a/lib/ansible/module_utils/facts/network/generic_bsd.py b/lib/ansible/module_utils/facts/network/generic_bsd.py index d211b29d8a3..54188638c60 100644 --- a/lib/ansible/module_utils/facts/network/generic_bsd.py +++ b/lib/ansible/module_utils/facts/network/generic_bsd.py @@ -34,18 +34,12 @@ class GenericBsdIfconfigNetwork(Network): def populate(self, collected_facts=None): network_facts = {} - ifconfig_path = self.module.get_bin_path( - 'ifconfig', - warning="skipping network facts" - ) + ifconfig_path = self.module.get_bin_path('ifconfig') if ifconfig_path is None: return network_facts - route_path = self.module.get_bin_path( - 'route', - warning="skipping network facts" - ) + route_path = self.module.get_bin_path('route') if route_path is None: return network_facts diff --git a/lib/ansible/module_utils/facts/network/hpux.py b/lib/ansible/module_utils/facts/network/hpux.py index 06cd8ed7353..5c8905a2763 100644 --- a/lib/ansible/module_utils/facts/network/hpux.py +++ b/lib/ansible/module_utils/facts/network/hpux.py @@ -31,8 +31,7 @@ class HPUXNetwork(Network): network_facts = {} netstat_path = self.module.get_bin_path( 'netstat', - opt_dirs=['/usr/bin'], - warning="skipping network facts" + opt_dirs=['/usr/bin'] ) if netstat_path is None: @@ -52,8 +51,7 @@ class HPUXNetwork(Network): default_interfaces = {} netstat_path = self.module.get_bin_path( 'netstat', - opt_dirs=['/usr/bin'], - warning="skipping default interface facts" + opt_dirs=['/usr/bin'] ) if netstat_path is None: @@ -73,8 +71,7 @@ class HPUXNetwork(Network): interfaces = {} netstat_path = self.module.get_bin_path( 'netstat', - opt_dirs=['/usr/bin'], - warning="skipping default interface info facts" + opt_dirs=['/usr/bin'] ) if netstat_path is None: diff --git a/lib/ansible/module_utils/facts/network/hurd.py b/lib/ansible/module_utils/facts/network/hurd.py index e3941587f67..05f23e5f445 100644 --- a/lib/ansible/module_utils/facts/network/hurd.py +++ b/lib/ansible/module_utils/facts/network/hurd.py @@ -63,10 +63,7 @@ class HurdPfinetNetwork(Network): def populate(self, collected_facts=None): network_facts = {} - fsysopts_path = self.module.get_bin_path( - 'fsysopts', - warning="skipping network facts" - ) + fsysopts_path = self.module.get_bin_path('fsysopts') if fsysopts_path is None: return network_facts diff --git a/lib/ansible/module_utils/facts/network/iscsi.py b/lib/ansible/module_utils/facts/network/iscsi.py index dddb19ee22c..48f98a682bd 100644 --- a/lib/ansible/module_utils/facts/network/iscsi.py +++ b/lib/ansible/module_utils/facts/network/iscsi.py @@ -79,10 +79,7 @@ class IscsiInitiatorNetworkCollector(NetworkCollector): iscsi_facts['iscsi_iqn'] = line.split('=', 1)[1] break elif sys.platform.startswith('aix'): - cmd = module.get_bin_path( - 'lsattr', - warning="skipping iscsi initiator facts" - ) + cmd = module.get_bin_path('lsattr') if cmd is None: return iscsi_facts @@ -95,8 +92,7 @@ class IscsiInitiatorNetworkCollector(NetworkCollector): elif sys.platform.startswith('hp-ux'): cmd = module.get_bin_path( 'iscsiutil', - opt_dirs=['/opt/iscsi/bin'], - warning="skipping iscsi initiator facts" + opt_dirs=['/opt/iscsi/bin'] ) if cmd is None: return iscsi_facts diff --git a/lib/ansible/module_utils/facts/other/facter.py b/lib/ansible/module_utils/facts/other/facter.py index e83f7355a94..41b3cea7c92 100644 --- a/lib/ansible/module_utils/facts/other/facter.py +++ b/lib/ansible/module_utils/facts/other/facter.py @@ -24,13 +24,11 @@ class FacterFactCollector(BaseFactCollector): def find_facter(self, module): facter_path = module.get_bin_path( 'facter', - opt_dirs=['/opt/puppetlabs/bin'], - warning="falling back to cfacter to gather facter facts" + opt_dirs=['/opt/puppetlabs/bin'] ) cfacter_path = module.get_bin_path( 'cfacter', - opt_dirs=['/opt/puppetlabs/bin'], - warning="skipping facter facts" + opt_dirs=['/opt/puppetlabs/bin'] ) # Prefer to use cfacter if available diff --git a/lib/ansible/module_utils/facts/other/ohai.py b/lib/ansible/module_utils/facts/other/ohai.py index 7aae1b7c84c..8f0e4dcaecb 100644 --- a/lib/ansible/module_utils/facts/other/ohai.py +++ b/lib/ansible/module_utils/facts/other/ohai.py @@ -37,8 +37,7 @@ class OhaiFactCollector(BaseFactCollector): def find_ohai(self, module): return module.get_bin_path( - 'ohai', - warning="skipping ohai facts" + 'ohai' ) def run_ohai(self, module, ohai_path): diff --git a/lib/ansible/module_utils/facts/sysctl.py b/lib/ansible/module_utils/facts/sysctl.py index 6cd065ae604..639e77c41f0 100644 --- a/lib/ansible/module_utils/facts/sysctl.py +++ b/lib/ansible/module_utils/facts/sysctl.py @@ -23,7 +23,7 @@ from ansible.module_utils.common.text.converters import to_text def get_sysctl(module, prefixes): sysctl = dict() - sysctl_cmd = module.get_bin_path('sysctl', warning='skipping sysctl based facts') + sysctl_cmd = module.get_bin_path('sysctl') if sysctl_cmd is not None: cmd = [sysctl_cmd] diff --git a/test/units/module_utils/basic/test_get_bin_path.py b/test/units/module_utils/basic/test_get_bin_path.py deleted file mode 100644 index bac656ea9c0..00000000000 --- a/test/units/module_utils/basic/test_get_bin_path.py +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright: Contributors to the Ansible project -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - -from __future__ import annotations - -import json - -import pytest - - -@pytest.mark.parametrize("stdin", [{}], indirect=["stdin"]) -def test_get_bin_path_warning(am, capfd): - am.get_bin_path("non_existent_cmd", warning="skipping non_existent_cmd") - - with pytest.raises(SystemExit): - am.exit_json() - out, dummy = capfd.readouterr() - expected_warning = ["Unable to find non_existent_cmd, skipping non_existent_cmd"] - assert json.loads(out)["warnings"] == expected_warning diff --git a/test/units/module_utils/facts/network/test_fc_wwn.py b/test/units/module_utils/facts/network/test_fc_wwn.py index ec22c19bbd2..fd0cc3e9288 100644 --- a/test/units/module_utils/facts/network/test_fc_wwn.py +++ b/test/units/module_utils/facts/network/test_fc_wwn.py @@ -89,7 +89,7 @@ FCMSUTIL_OUT = """ """ -def mock_get_bin_path(cmd, required=False, opt_dirs=None, warning=None): +def mock_get_bin_path(cmd, required=False, opt_dirs=None): cmds = { 'lsdev': '/usr/sbin/lsdev', 'lscfg': '/usr/sbin/lscfg', diff --git a/test/units/module_utils/facts/network/test_generic_bsd.py b/test/units/module_utils/facts/network/test_generic_bsd.py index c66b00cc56d..3b26f0a0e1f 100644 --- a/test/units/module_utils/facts/network/test_generic_bsd.py +++ b/test/units/module_utils/facts/network/test_generic_bsd.py @@ -9,7 +9,7 @@ import pytest from ansible.module_utils.facts.network import generic_bsd -def mock_get_bin_path(command, warning=None): +def mock_get_bin_path(command): cmds = { 'ifconfig': 'fake/ifconfig', 'route': 'fake/route',