def get_distribution_codename(): ''' Return the code name for this Linux Distribution :rtype: NativeString or None :returns: A string representation of the distribution's codename or None if not a Linux distro ''' codename = None if platform.system() == 'Linux': # Until this gets merged and we update our bundled copy of distro: # https://github.com/nir0s/distro/pull/230 # Fixes Fedora 28+ not having a code name and Ubuntu Xenial Xerus needing to be "xenial" os_release_info = distro.os_release_info() codename = os_release_info.get('version_codename') if codename is None: codename = os_release_info.get('ubuntu_codename') if codename is None and distro.id() == 'ubuntu': lsb_release_info = distro.lsb_release_info() codename = lsb_release_info.get('codename') if codename is None: codename = distro.codename() if codename == u'': codename = None return codename
def __init__(self, module): """ Initialize all needed Variables """ self.module = module self.state = module.params.get("state") self.package_version = module.params.get("package_version") self.package_name = module.params.get("package_name") self.repository = module.params.get("repository") self.distribution = distro.id() self.version = distro.version() self.codename = distro.codename()
def get_distribution_version(): ''' Get the version of the Linux distribution the code is running on :rtype: NativeString or None :returns: A string representation of the version of the distribution. If it cannot determine the version, it returns empty string. If this is not run on a Linux machine it returns None ''' version = None needs_best_version = frozenset(( u'centos', u'debian', )) if platform.system() == 'Linux': version = distro.version() distro_id = distro.id() if version is not None: if distro_id in needs_best_version: version_best = distro.version(best=True) # CentoOS maintainers believe only the major version is appropriate # but Ansible users desire minor version information, e.g., 7.5. # https://github.com/ansible/ansible/issues/50141#issuecomment-449452781 if distro_id == u'centos': version = u'.'.join(version_best.split(u'.')[:2]) # Debian does not include minor version in /etc/os-release. # Bug report filed upstream requesting this be added to /etc/os-release # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=931197 if distro_id == u'debian': version = version_best else: version = u'' return version
def get_distribution(): ''' Return the name of the distribution the module is running on. :rtype: NativeString or None :returns: Name of the distribution the module is running on This function attempts to determine what distribution the code is running on and return a string representing that value. If the platform is Linux and the distribution cannot be determined, it returns ``OtherLinux``. ''' distribution = distro.id().capitalize() if platform.system() == 'Linux': if distribution == 'Amzn': distribution = 'Amazon' elif distribution == 'Rhel': distribution = 'Redhat' elif not distribution: distribution = 'OtherLinux' return distribution
def test_id(self): id = distro.id() assert isinstance( id, string_types ), 'distro.id() returned %s (%s) which is not a string' % (id, type(id))
def test_opensuse_leap_id(self): name = distro.name() if name == 'openSUSE Leap': id = distro.id() assert id == 'opensuse', "OpenSUSE Leap did not return 'opensuse' as id"
'/etc/os-release', '/etc/coreos/update.conf', '/etc/flatcar/update.conf', '/usr/lib/os-release', ] fcont = {} for f in filelist: if os.path.exists(f): s = os.path.getsize(f) if s > 0 and s < 10000: with open(f) as fh: fcont[f] = fh.read() dist = (distro.id(), distro.version(), distro.codename()) facts = [ 'distribution', 'distribution_version', 'distribution_release', 'distribution_major_version', 'os_family' ] try: b_ansible_out = subprocess.check_output( ['ansible', 'localhost', '-m', 'setup']) except subprocess.CalledProcessError as e: print("ERROR: ansible run failed, output was: \n") print(e.output) sys.exit(e.returncode) ansible_out = to_text(b_ansible_out)
ansible_out = to_text(b_ansible_out) parsed = json.loads(ansible_out[ansible_out.index('{'):]) ansible_facts = {} for fact in facts: try: ansible_facts[fact] = parsed['ansible_facts']['ansible_' + fact] except Exception: ansible_facts[fact] = "N/A" nicename = ansible_facts['distribution'] + ' ' + ansible_facts[ 'distribution_version'] output = { 'name': nicename, 'distro': { 'codename': distro.codename(), 'id': distro.id(), 'name': distro.name(), 'version': distro.version(), 'version_best': distro.version(best=True), 'lsb_release_info': distro.lsb_release_info(), 'os_release_info': distro.os_release_info(), }, 'input': fcont, 'platform.dist': dist, 'result': ansible_facts, } print(json.dumps(output, indent=4))