"""OS identification and explicit profile checks; never probes a remote host.""" from pathlib import Path import platform import shlex PROFILES = [ ('Auto-detect', 'auto'), ('Windows Server 2016', 'windows:2016'), ('Windows Server 2019', 'windows:2019'), ('Windows Server 2022', 'windows:2022'), ('Windows Server 2025', 'windows:2025'), ('Red Hat Enterprise Linux 7 (legacy)', 'rhel:7'), ('Red Hat Enterprise Linux 8', 'rhel:8'), ('Red Hat Enterprise Linux 9', 'rhel:9'), ('Red Hat Enterprise Linux 10', 'rhel:10'), ('RHEL-compatible Linux', 'rhel:any'), ] def detect_local_os(): system = platform.system() if system == 'Linux': release = {} for line in Path('/etc/os-release').read_text(encoding='utf-8').splitlines(): if '=' in line: key, value = line.split('=', 1) release[key] = ' '.join(shlex.split(value)) distro = release.get('ID', '') related = set(release.get('ID_LIKE', '').split()) rhel = distro in {'rhel', 'rocky', 'almalinux', 'centos', 'ol'} or 'rhel' in related return {'platform': 'linux', 'family': 'rhel' if rhel else 'linux', 'version': release.get('VERSION_ID', '').split('.')[0], 'name': release.get('PRETTY_NAME', 'Linux')} if system == 'Windows': # Do not infer a Server edition from a desktop kernel/build number. return {'platform': 'windows', 'family': 'windows', 'version': '', 'name': 'Windows ' + platform.win32_ver()[0]} raise ValueError('Local collection is available on Linux. Import an inventory file for other platforms.') def check_profile(detected, profile): if profile == 'auto': return valid = {value for _, value in PROFILES} if profile not in valid: raise ValueError('Unknown operating system profile.') family, version = profile.split(':') if detected['family'] != family or (version != 'any' and detected['version'] != version): raise ValueError(f"Detected {detected['name']}, which does not match the selected profile. Select Auto-detect to assess this computer.")