From c53c6a507aa51f3e73052b19f287557df175f64c Mon Sep 17 00:00:00 2001 From: Ruggero Marchei Date: Sat, 14 Jun 2014 19:29:04 +0200 Subject: [PATCH 1/3] use different strategy for RHEL >= 7. Fixes 7763 --- library/system/hostname | 117 +++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 43 deletions(-) diff --git a/library/system/hostname b/library/system/hostname index 61bdf985b99..66d8d6c8159 100644 --- a/library/system/hostname +++ b/library/system/hostname @@ -39,6 +39,22 @@ EXAMPLES = ''' - hostname: name=web01 ''' +import platform + + +def get_distribution_version(): + ''' return the distribution version ''' + if platform.system() == 'Linux': + try: + distribution_version = platform.linux_distribution()[1] + except: + # FIXME: MethodMissing, I assume? + distribution_version = platform.dist()[1] + else: + distribution_version = None + return distribution_version + + class UnimplementedStrategy(object): def __init__(self, module): self.module = module @@ -135,6 +151,7 @@ class GenericStrategy(object): def set_permanent_hostname(self, name): pass + # =========================================== class DebianStrategy(GenericStrategy): @@ -173,20 +190,6 @@ class DebianStrategy(GenericStrategy): self.module.fail_json(msg="failed to update hostname: %s" % str(err)) -class DebianHostname(Hostname): - platform = 'Linux' - distribution = 'Debian' - strategy_class = DebianStrategy - -class UbuntuHostname(Hostname): - platform = 'Linux' - distribution = 'Ubuntu' - strategy_class = DebianStrategy - -class LinaroHostname(Hostname): - platform = 'Linux' - distribution = 'Linaro' - strategy_class = DebianStrategy # =========================================== @@ -236,35 +239,6 @@ class RedHatStrategy(GenericStrategy): self.module.fail_json(msg="failed to update hostname: %s" % str(err)) -class RedHat5Hostname(Hostname): - platform = 'Linux' - distribution = 'Redhat' - strategy_class = RedHatStrategy - -class RedHatServerHostname(Hostname): - platform = 'Linux' - distribution = 'Red hat enterprise linux server' - strategy_class = RedHatStrategy - -class RedHatWorkstationHostname(Hostname): - platform = 'Linux' - distribution = 'Red hat enterprise linux workstation' - strategy_class = RedHatStrategy - -class CentOSHostname(Hostname): - platform = 'Linux' - distribution = 'Centos' - strategy_class = RedHatStrategy - -class AmazonLinuxHostname(Hostname): - platform = 'Linux' - distribution = 'Amazon' - strategy_class = RedHatStrategy - -class ScientificLinuxHostname(Hostname): - platform = 'Linux' - distribution = 'Scientific' - strategy_class = RedHatStrategy # =========================================== @@ -324,6 +298,63 @@ class ArchHostname(Hostname): distribution = 'Arch' strategy_class = FedoraStrategy +class RedHat5Hostname(Hostname): + platform = 'Linux' + distribution = 'Redhat' + strategy_class = RedHatStrategy + +class RedHatServerHostname(Hostname): + platform = 'Linux' + distribution = 'Red hat enterprise linux server' + if float(get_distribution_version()) >= 7: + strategy_class = FedoraStrategy + else: + strategy_class = RedHatStrategy + +class RedHatWorkstationHostname(Hostname): + platform = 'Linux' + distribution = 'Red hat enterprise linux workstation' + if float(get_distribution_version()) >= 7: + strategy_class = FedoraStrategy + else: + strategy_class = RedHatStrategy + +class CentOSHostname(Hostname): + platform = 'Linux' + distribution = 'Centos' + if float(get_distribution_version()) >= 7: + strategy_class = FedoraStrategy + else: + strategy_class = RedHatStrategy + +class ScientificLinuxHostname(Hostname): + platform = 'Linux' + distribution = 'Scientific' + if float(get_distribution_version()) >= 7: + strategy_class = FedoraStrategy + else: + strategy_class = RedHatStrategy + +class AmazonLinuxHostname(Hostname): + platform = 'Linux' + distribution = 'Amazon' + strategy_class = RedHatStrategy + +class DebianHostname(Hostname): + platform = 'Linux' + distribution = 'Debian' + strategy_class = DebianStrategy + +class UbuntuHostname(Hostname): + platform = 'Linux' + distribution = 'Ubuntu' + strategy_class = DebianStrategy + +class LinaroHostname(Hostname): + platform = 'Linux' + distribution = 'Linaro' + strategy_class = DebianStrategy + # =========================================== def main(): From e3f5af377f669496ff7bd42c2c9a0d9ebc3332a6 Mon Sep 17 00:00:00 2001 From: Ruggero Marchei Date: Mon, 16 Jun 2014 15:06:50 +0200 Subject: [PATCH 2/3] move get_distribution_version() to basic.py --- lib/ansible/module_utils/basic.py | 12 ++++++++++++ library/system/hostname | 21 +++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) mode change 100644 => 100755 library/system/hostname diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 156aa480a07..ed3a1d74783 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -142,6 +142,18 @@ def get_distribution(): distribution = None return distribution +def get_distribution_version(): + ''' return the distribution version ''' + if platform.system() == 'Linux': + try: + distribution_version = platform.linux_distribution()[1] + except: + # FIXME: MethodMissing, I assume? + distribution_version = platform.dist()[1] + else: + distribution_version = None + return distribution_version + def load_platform_subclass(cls, *args, **kwargs): ''' used by modules like User to have different implementations based on detected platform. See User diff --git a/library/system/hostname b/library/system/hostname old mode 100644 new mode 100755 index 66d8d6c8159..775b47a1ff9 --- a/library/system/hostname +++ b/library/system/hostname @@ -39,20 +39,8 @@ EXAMPLES = ''' - hostname: name=web01 ''' -import platform - - -def get_distribution_version(): - ''' return the distribution version ''' - if platform.system() == 'Linux': - try: - distribution_version = platform.linux_distribution()[1] - except: - # FIXME: MethodMissing, I assume? - distribution_version = platform.dist()[1] - else: - distribution_version = None - return distribution_version +# import module snippets +from ansible.module_utils.basic import * class UnimplementedStrategy(object): @@ -283,6 +271,9 @@ class FedoraStrategy(GenericStrategy): self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" % (rc, out, err)) + +# =========================================== + class FedoraHostname(Hostname): platform = 'Linux' distribution = 'Fedora' @@ -380,6 +371,4 @@ def main(): module.exit_json(changed=changed, name=name) -# import module snippets -from ansible.module_utils.basic import * main() From 2493020d9ff6421666d090d7bb082e92b3613d3e Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 18 Jun 2014 13:10:39 -0500 Subject: [PATCH 3/3] Fix get_distribution() for platforms that may have spaces The new CentOS 7 beta lists the distribution as "CentOS Linux", which breaks the distribution detection and class loading. This patch fixes that by taking just the first entry in the string when a space is detected. --- lib/ansible/module_utils/basic.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index ed3a1d74783..8d1b62af755 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -129,6 +129,8 @@ def get_distribution(): if platform.system() == 'Linux': try: distribution = platform.linux_distribution()[0].capitalize() + if " " in distribution: + distribution = distribution.split()[0] if not distribution and os.path.isfile('/etc/system-release'): distribution = platform.linux_distribution(supported_dists=['system'])[0].capitalize() if 'Amazon' in distribution: