From fb66b4ffbc273402862444e7816bfa1c24195709 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Fri, 5 Mar 2021 19:46:10 +0530 Subject: [PATCH] distribution: Add Amazon Linux distribution facts (#73767) * Update Amazon Linux Distribution facts gathering logic * Update tests Fixes: #73742 Signed-off-by: Abhijeet Kasurde --- .../fragments/73742_amazon_distribution.yml | 2 + .../module_utils/facts/system/distribution.py | 15 +++++-- .../distribution/fixtures/amazon_linux_2.json | 34 ++++++++++++++++ .../fixtures/amazon_linux_2016.03.json | 40 +++++++++++++++++++ .../fixtures/amazon_linux_2018.03.json | 40 +++++++++++++++++++ .../fixtures/amazon_linux_2_karoo.json | 34 ++++++++++++++++ 6 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/73742_amazon_distribution.yml create mode 100644 test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2.json create mode 100644 test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2016.03.json create mode 100644 test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2018.03.json create mode 100644 test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2_karoo.json diff --git a/changelogs/fragments/73742_amazon_distribution.yml b/changelogs/fragments/73742_amazon_distribution.yml new file mode 100644 index 00000000000..9248e8a6f93 --- /dev/null +++ b/changelogs/fragments/73742_amazon_distribution.yml @@ -0,0 +1,2 @@ +minor_changes: +- distribution - add facts about Amazon Linux Distribution facts (https://github.com/ansible/ansible/issues/73742). diff --git a/lib/ansible/module_utils/facts/system/distribution.py b/lib/ansible/module_utils/facts/system/distribution.py index fd49b3a6fbd..751d1d35a1c 100644 --- a/lib/ansible/module_utils/facts/system/distribution.py +++ b/lib/ansible/module_utils/facts/system/distribution.py @@ -59,6 +59,7 @@ class DistributionFiles: {'path': '/etc/redhat-release', 'name': 'RedHat'}, {'path': '/etc/vmware-release', 'name': 'VMwareESX', 'allowempty': True}, {'path': '/etc/openwrt_release', 'name': 'OpenWrt'}, + {'path': '/etc/os-release', 'name': 'Amazon'}, {'path': '/etc/system-release', 'name': 'Amazon'}, {'path': '/etc/alpine-release', 'name': 'Alpine'}, {'path': '/etc/arch-release', 'name': 'Archlinux', 'allowempty': True}, @@ -222,9 +223,17 @@ class DistributionFiles: if 'Amazon' not in data: return False, amazon_facts amazon_facts['distribution'] = 'Amazon' - version = [n for n in data.split() if n.isdigit()] - version = version[0] if version else 'NA' - amazon_facts['distribution_version'] = version + if path == '/etc/os-release': + version = re.search(r"VERSION_ID=\"(.*)\"", data) + if version: + amazon_facts['distribution_version'] = version.group(1) + amazon_facts['distribution_major_version'] = version.group(1).split('.')[0] + amazon_facts['distribution_minor_version'] = version.group(1).split('.')[1] + else: + version = [n for n in data.split() if n.isdigit()] + version = version[0] if version else 'NA' + amazon_facts['distribution_version'] = version + return True, amazon_facts def parse_distribution_file_OpenWrt(self, name, data, path, collected_facts): diff --git a/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2.json b/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2.json new file mode 100644 index 00000000000..9fa609068cd --- /dev/null +++ b/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2.json @@ -0,0 +1,34 @@ +{ + "platform.dist": [ + "", + "", + "" + ], + "input": { + "/etc/system-release": "Amazon Linux release 2", + "/etc/os-release": "" + }, + "name": "Amazon Linux 2", + "result": { + "distribution_release": "NA", + "distribution": "Amazon", + "distribution_major_version": "2", + "os_family": "RedHat", + "distribution_version": "2" + }, + "distro": { + "id": "amzn", + "version": "2", + "codename": "", + "os_release_info": { + "name": "Amazon Linux AMI", + "ansi_color": "0;33", + "id_like": "rhel fedora", + "version_id": "2", + "pretty_name": "Amazon Linux release 2", + "version": "2", + "home_url": "", + "id": "amzn" + } + } +} \ No newline at end of file diff --git a/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2016.03.json b/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2016.03.json new file mode 100644 index 00000000000..38449e407da --- /dev/null +++ b/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2016.03.json @@ -0,0 +1,40 @@ +{ + "name": "Amazon 2016.03", + "platform.release": "4.14.94-73.73.amzn1.x86_64", + "result": { + "distribution_release": "NA", + "distribution": "Amazon", + "distribution_major_version": "2016", + "distribution_minor_version": "03", + "os_family": "RedHat", + "distribution_version": "2016.03" + }, + "platform.dist": [ + "amzn", + "2016.03", + "" + ], + "input": { + "/etc/os-release": "NAME=\"Amazon Linux AMI\"\nVERSION=\"2016.03\"\nID=\"amzn\"\nID_LIKE=\"rhel fedora\"\nVERSION_ID=\"2016.03\"\nPRETTY_NAME=\"Amazon Linux AMI 2016.03\"\nANSI_COLOR=\"0;33\"\nCPE_NAME=\"cpe:/o:amazon:linux:2016.03:ga\"\nHOME_URL=\"http://aws.amazon.com/amazon-linux-ami/\"\n", + "/etc/system-release": "Amazon Linux AMI release 2016.03\n" + }, + "distro": { + "version_best": "2016.03", + "os_release_info": { + "name": "Amazon Linux AMI", + "ansi_color": "0;33", + "id_like": "rhel fedora", + "version_id": "2016.03", + "pretty_name": "Amazon Linux AMI 2016.03", + "version": "2016.03", + "home_url": "http://aws.amazon.com/amazon-linux-ami/", + "cpe_name": "cpe:/o:amazon:linux:2016.03:ga", + "id": "amzn" + }, + "version": "2016.03", + "codename": "", + "lsb_release_info": {}, + "id": "amzn", + "name": "Amazon Linux AMI" + } +} \ No newline at end of file diff --git a/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2018.03.json b/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2018.03.json new file mode 100644 index 00000000000..2461e72d65b --- /dev/null +++ b/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2018.03.json @@ -0,0 +1,40 @@ +{ + "name": "Amazon 2018.03", + "platform.release": "4.14.94-73.73.amzn1.x86_64", + "result": { + "distribution_release": "NA", + "distribution": "Amazon", + "distribution_major_version": "2018", + "distribution_minor_version": "03", + "os_family": "RedHat", + "distribution_version": "2018.03" + }, + "platform.dist": [ + "amzn", + "2018.03", + "" + ], + "input": { + "/etc/os-release": "NAME=\"Amazon Linux AMI\"\nVERSION=\"2018.03\"\nID=\"amzn\"\nID_LIKE=\"rhel fedora\"\nVERSION_ID=\"2018.03\"\nPRETTY_NAME=\"Amazon Linux AMI 2018.03\"\nANSI_COLOR=\"0;33\"\nCPE_NAME=\"cpe:/o:amazon:linux:2018.03:ga\"\nHOME_URL=\"http://aws.amazon.com/amazon-linux-ami/\"\n", + "/etc/system-release": "Amazon Linux AMI release 2018.03\n" + }, + "distro": { + "version_best": "2018.03", + "os_release_info": { + "name": "Amazon Linux AMI", + "ansi_color": "0;33", + "id_like": "rhel fedora", + "version_id": "2018.03", + "pretty_name": "Amazon Linux AMI 2018.03", + "version": "2018.03", + "home_url": "http://aws.amazon.com/amazon-linux-ami/", + "cpe_name": "cpe:/o:amazon:linux:2018.03:ga", + "id": "amzn" + }, + "version": "2018.03", + "codename": "", + "lsb_release_info": {}, + "id": "amzn", + "name": "Amazon Linux AMI" + } +} \ No newline at end of file diff --git a/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2_karoo.json b/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2_karoo.json new file mode 100644 index 00000000000..e430ff62b53 --- /dev/null +++ b/test/units/module_utils/facts/system/distribution/fixtures/amazon_linux_2_karoo.json @@ -0,0 +1,34 @@ +{ + "platform.dist": [ + "", + "", + "" + ], + "input": { + "/etc/system-release": "Amazon Linux release 2 (Karoo)", + "/etc/os-release": "" + }, + "name": "Amazon Linux 2 - Karoo", + "result": { + "distribution_release": "NA", + "distribution": "Amazon", + "distribution_major_version": "2", + "os_family": "RedHat", + "distribution_version": "2" + }, + "distro": { + "id": "amzn", + "version": "2", + "codename": "", + "os_release_info": { + "name": "Amazon Linux AMI", + "ansi_color": "0;33", + "id_like": "rhel fedora", + "version_id": "2", + "pretty_name": "Amazon Linux release 2 (Karoo)", + "version": "2", + "home_url": "https://amazonlinux.com/", + "id": "amzn" + } + } +} \ No newline at end of file