Improve hostname module to support systemd in more generic way (#4382)

Now that there is general purpose `Fact` helper to detect if systemd
is active, we would be able to rely on that to apply SystemdStrategy.

Detecting presence of systemd at runtime would be more reliable than
distribution version based heuristics. (e.g., Debian, Ubuntu allows
user to change the default init system, Gentoo allows switching as
well, and so on).
pull/18777/head
Indrajit Raychaudhuri 8 years ago committed by Matt Clay
parent ab3a87cc1a
commit fb255b3607

@ -48,6 +48,7 @@ from distutils.version import LooseVersion
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
from ansible.module_utils.facts import *
class UnimplementedStrategy(object): class UnimplementedStrategy(object):
@ -94,9 +95,12 @@ class Hostname(object):
return load_platform_subclass(Hostname, args, kwargs) return load_platform_subclass(Hostname, args, kwargs)
def __init__(self, module): def __init__(self, module):
self.module = module self.module = module
self.name = module.params['name'] self.name = module.params['name']
self.strategy = self.strategy_class(module) if self.platform == 'Linux' and Facts(module).is_systemd_managed():
self.strategy = SystemdStrategy(module)
else:
self.strategy = self.strategy_class(module)
def get_current_hostname(self): def get_current_hostname(self):
return self.strategy.get_current_hostname() return self.strategy.get_current_hostname()
@ -512,9 +516,7 @@ class SLESHostname(Hostname):
platform = 'Linux' platform = 'Linux'
distribution = 'Suse linux enterprise server ' distribution = 'Suse linux enterprise server '
distribution_version = get_distribution_version() distribution_version = get_distribution_version()
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("12"): if distribution_version and LooseVersion("10") <= LooseVersion(distribution_version) <= LooseVersion("12"):
strategy_class = SystemdStrategy
elif distribution_version and LooseVersion("10") <= LooseVersion(distribution_version) <= LooseVersion("12"):
strategy_class = SLESStrategy strategy_class = SLESStrategy
else: else:
strategy_class = UnimplementedStrategy strategy_class = UnimplementedStrategy
@ -537,56 +539,37 @@ class RedHat5Hostname(Hostname):
class RedHatServerHostname(Hostname): class RedHatServerHostname(Hostname):
platform = 'Linux' platform = 'Linux'
distribution = 'Red hat enterprise linux server' distribution = 'Red hat enterprise linux server'
distribution_version = get_distribution_version() strategy_class = RedHatStrategy
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
strategy_class = SystemdStrategy
else:
strategy_class = RedHatStrategy
class RedHatWorkstationHostname(Hostname): class RedHatWorkstationHostname(Hostname):
platform = 'Linux' platform = 'Linux'
distribution = 'Red hat enterprise linux workstation' distribution = 'Red hat enterprise linux workstation'
distribution_version = get_distribution_version() strategy_class = RedHatStrategy
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
strategy_class = SystemdStrategy
else:
strategy_class = RedHatStrategy
class CentOSHostname(Hostname): class CentOSHostname(Hostname):
platform = 'Linux' platform = 'Linux'
distribution = 'Centos' distribution = 'Centos'
distribution_version = get_distribution_version() strategy_class = RedHatStrategy
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
strategy_class = SystemdStrategy
else:
strategy_class = RedHatStrategy
class CentOSLinuxHostname(Hostname): class CentOSLinuxHostname(Hostname):
platform = 'Linux' platform = 'Linux'
distribution = 'Centos linux' distribution = 'Centos linux'
distribution_version = get_distribution_version() strategy_class = RedHatStrategy
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
strategy_class = SystemdStrategy
else:
strategy_class = RedHatStrategy
class ScientificHostname(Hostname): class ScientificHostname(Hostname):
platform = 'Linux' platform = 'Linux'
distribution = 'Scientific' distribution = 'Scientific'
distribution_version = get_distribution_version() strategy_class = RedHatStrategy
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
strategy_class = SystemdStrategy
else:
strategy_class = RedHatStrategy
class ScientificLinuxHostname(Hostname): class ScientificLinuxHostname(Hostname):
platform = 'Linux' platform = 'Linux'
distribution = 'Scientific linux' distribution = 'Scientific linux'
distribution_version = get_distribution_version() strategy_class = RedHatStrategy
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
strategy_class = SystemdStrategy class OracleLinuxHostname(Hostname):
else: platform = 'Linux'
strategy_class = RedHatStrategy distribution = 'Oracle linux server'
strategy_class = RedHatStrategy
class AmazonLinuxHostname(Hostname): class AmazonLinuxHostname(Hostname):
platform = 'Linux' platform = 'Linux'
@ -649,7 +632,7 @@ class FreeBSDHostname(Hostname):
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
name=dict(required=True, type='str') name=dict(required=True)
) )
) )
@ -673,4 +656,5 @@ def main():
ansible_fqdn=socket.getfqdn(), ansible_fqdn=socket.getfqdn(),
ansible_domain='.'.join(socket.getfqdn().split('.')[1:]))) ansible_domain='.'.join(socket.getfqdn().split('.')[1:])))
main() if __name__ == '__main__':
main()

Loading…
Cancel
Save