diff --git a/changelogs/fragments/74481_debian_minor_version.yml b/changelogs/fragments/74481_debian_minor_version.yml new file mode 100644 index 00000000000..bd449f782a7 --- /dev/null +++ b/changelogs/fragments/74481_debian_minor_version.yml @@ -0,0 +1,2 @@ +minor_changes: +- distribution - add distribution_minor_version for Debian Distro (https://github.com/ansible/ansible/issues/74481). diff --git a/lib/ansible/module_utils/facts/system/distribution.py b/lib/ansible/module_utils/facts/system/distribution.py index 58351044474..90df63d4a24 100644 --- a/lib/ansible/module_utils/facts/system/distribution.py +++ b/lib/ansible/module_utils/facts/system/distribution.py @@ -14,7 +14,7 @@ import ansible.module_utils.compat.typing as t from ansible.module_utils.common.sys_info import get_distribution, get_distribution_version, \ get_distribution_codename -from ansible.module_utils.facts.utils import get_file_content +from ansible.module_utils.facts.utils import get_file_content, get_file_lines from ansible.module_utils.facts.collector import BaseFactCollector @@ -334,6 +334,12 @@ class DistributionFiles: rc, out, err = self.module.run_command(cmd) if rc == 0: debian_facts['distribution_release'] = out.strip() + debian_version_path = '/etc/debian_version' + distdata = get_file_lines(debian_version_path) + for line in distdata: + m = re.search(r'(\d+)\.(\d+)', line.strip()) + if m: + debian_facts['distribution_minor_version'] = m.groups()[1] elif 'Ubuntu' in data: debian_facts['distribution'] = 'Ubuntu' # nothing else to do, Ubuntu gets correct info from python functions diff --git a/test/units/module_utils/facts/system/distribution/fixtures/debian_10.json b/test/units/module_utils/facts/system/distribution/fixtures/debian_10.json index 20544c1e67e..5ac3f457fc5 100644 --- a/test/units/module_utils/facts/system/distribution/fixtures/debian_10.json +++ b/test/units/module_utils/facts/system/distribution/fixtures/debian_10.json @@ -27,7 +27,8 @@ }, "input": { "/etc/os-release": "PRETTY_NAME=\"Debian GNU/Linux 10 (buster)\"\nNAME=\"Debian GNU/Linux\"\nVERSION_ID=\"10\"\nVERSION=\"10 (buster)\"\nVERSION_CODENAME=buster\nID=debian\nHOME_URL=\"https://www.debian.org/\"\nSUPPORT_URL=\"https://www.debian.org/support\"\nBUG_REPORT_URL=\"https://bugs.debian.org/\"\n", - "/usr/lib/os-release": "PRETTY_NAME=\"Debian GNU/Linux 10 (buster)\"\nNAME=\"Debian GNU/Linux\"\nVERSION_ID=\"10\"\nVERSION=\"10 (buster)\"\nVERSION_CODENAME=buster\nID=debian\nHOME_URL=\"https://www.debian.org/\"\nSUPPORT_URL=\"https://www.debian.org/support\"\nBUG_REPORT_URL=\"https://bugs.debian.org/\"\n" + "/usr/lib/os-release": "PRETTY_NAME=\"Debian GNU/Linux 10 (buster)\"\nNAME=\"Debian GNU/Linux\"\nVERSION_ID=\"10\"\nVERSION=\"10 (buster)\"\nVERSION_CODENAME=buster\nID=debian\nHOME_URL=\"https://www.debian.org/\"\nSUPPORT_URL=\"https://www.debian.org/support\"\nBUG_REPORT_URL=\"https://bugs.debian.org/\"\n", + "/etc/debian_version": "10.7\n" }, "platform.dist": ["debian", "10", "buster"], "result": { @@ -35,6 +36,7 @@ "distribution_version": "10", "distribution_release": "buster", "distribution_major_version": "10", + "distribution_minor_version": "7", "os_family": "Debian" } -} \ No newline at end of file +} diff --git a/test/units/module_utils/facts/system/distribution/test_distribution_version.py b/test/units/module_utils/facts/system/distribution/test_distribution_version.py index 161ca728f63..a990274a6d3 100644 --- a/test/units/module_utils/facts/system/distribution/test_distribution_version.py +++ b/test/units/module_utils/facts/system/distribution/test_distribution_version.py @@ -50,6 +50,13 @@ def test_distribution_version(am, mocker, testcase): data = data.strip() return data + def mock_get_file_lines(fname, strip=True): + """give fake lines if file exists, otherwise return empty list""" + data = mock_get_file_content(fname=fname, strip=strip) + if data: + return [data] + return [] + def mock_get_uname(am, flags): if '-v' in flags: return testcase.get('uname_v', None) @@ -115,6 +122,7 @@ def test_distribution_version(am, mocker, testcase): return ret mocker.patch('ansible.module_utils.facts.system.distribution.get_file_content', mock_get_file_content) + mocker.patch('ansible.module_utils.facts.system.distribution.get_file_lines', mock_get_file_lines) mocker.patch('ansible.module_utils.facts.system.distribution.get_uname', mock_get_uname) mocker.patch('ansible.module_utils.facts.system.distribution._file_exists', mock_file_exists) mocker.patch('ansible.module_utils.distro.name', mock_distro_name)