Skip to content

Commit ab6a9ef

Browse files
authored
Get minor version number for CentOS and Debian (ansible#57814)
* Get the most detailed version number from distro.version() for CentOS and Debian * Update tests and fixtures * Update fixture generation script to gather distro info and work with Python 3 * Update LinuxMint fixtures * Cleanup fixture formatting * Improvements based on feedback from abadger: - use unicode since that is what distro returns - use frozenset with a tuple - include link Debian to bug
1 parent 2f91266 commit ab6a9ef

File tree

3 files changed

+514
-305
lines changed

3 files changed

+514
-305
lines changed

hacking/tests/gen_distribution_version_testcase.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import sys
1717

1818
from ansible.module_utils import distro
19+
from ansible.module_utils._text import to_text
1920

2021

2122
filelist = [
@@ -53,13 +54,14 @@
5354
facts = ['distribution', 'distribution_version', 'distribution_release', 'distribution_major_version', 'os_family']
5455

5556
try:
56-
ansible_out = subprocess.check_output(
57+
b_ansible_out = subprocess.check_output(
5758
['ansible', 'localhost', '-m', 'setup'])
5859
except subprocess.CalledProcessError as e:
5960
print("ERROR: ansible run failed, output was: \n")
6061
print(e.output)
6162
sys.exit(e.returncode)
6263

64+
ansible_out = to_text(b_ansible_out)
6365
parsed = json.loads(ansible_out[ansible_out.index('{'):])
6466
ansible_facts = {}
6567
for fact in facts:
@@ -72,6 +74,13 @@
7274

7375
output = {
7476
'name': nicename,
77+
'distro': {
78+
'codename': distro.codename(),
79+
'id': distro.id(),
80+
'name': distro.name(),
81+
'version': distro.version(),
82+
'version_best': distro.version(best=True),
83+
},
7584
'input': fcont,
7685
'platform.dist': dist,
7786
'result': ansible_facts,

lib/ansible/module_utils/common/sys_info.py

+27
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,35 @@ def get_distribution_version():
5050
the version, it returns empty string. If this is not run on a Linux machine it returns None
5151
'''
5252
version = None
53+
54+
needs_best_version = frozenset((
55+
u'centos',
56+
u'debian',
57+
))
58+
5359
if platform.system() == 'Linux':
5460
version = distro.version()
61+
distro_id = distro.id()
62+
63+
if version is not None:
64+
if distro_id in needs_best_version:
65+
version_best = distro.version(best=True)
66+
67+
# CentoOS maintainers believe only the major version is appropriate
68+
# but Ansible users desire minor version information, e.g., 7.5.
69+
# https://github.com/ansible/ansible/issues/50141#issuecomment-449452781
70+
if distro_id == u'centos':
71+
version = u'.'.join(version_best.split(u'.')[:2])
72+
73+
# Debian does not include minor version in /etc/os-release.
74+
# Bug report filed upstream requesting this be added to /etc/os-release
75+
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=931197
76+
if distro_id == u'debian':
77+
version = version_best
78+
79+
else:
80+
version = u''
81+
5582
return version
5683

5784

0 commit comments

Comments
 (0)