From 4f78573a9959b0902c41bd1b64208153e69cb3c2 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 26 Jun 2019 10:39:28 -0400 Subject: [PATCH] Added 'use' option to hostname (#56679) * Added 'use' option to hostname fixes #25543 --- changelogs/fragments/hostname_use.yml | 2 ++ lib/ansible/modules/system/hostname.py | 33 ++++++++++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/hostname_use.yml diff --git a/changelogs/fragments/hostname_use.yml b/changelogs/fragments/hostname_use.yml new file mode 100644 index 00000000000..bcf4e836255 --- /dev/null +++ b/changelogs/fragments/hostname_use.yml @@ -0,0 +1,2 @@ +minor_changes: + - added ``use`` option to ``hostname`` module to allow user to override autodetection. diff --git a/lib/ansible/modules/system/hostname.py b/lib/ansible/modules/system/hostname.py index e064b23fe85..d53b439893c 100644 --- a/lib/ansible/modules/system/hostname.py +++ b/lib/ansible/modules/system/hostname.py @@ -29,6 +29,12 @@ options: description: - Name of the host required: true + use: + description: + - Which strategy to use to update the hostname. + - If not set we try to autodetect, but this can be problematic, specially with containers as they can present misleading information. + choices: ['generic', 'debian','sles', 'redhat', 'alpine', 'systemd', 'openrc', 'openbsd', 'solaris', 'freebsd'] + version_added: '2.9' ''' EXAMPLES = ''' @@ -50,6 +56,9 @@ from ansible.module_utils.basic import ( from ansible.module_utils.facts.system.service_mgr import ServiceMgrFactCollector from ansible.module_utils._text import to_native +STRATS = {'generic': 'Generic', 'debian': 'Debian', 'sles': 'SLES', 'redhat': 'RedHat', 'alpine': 'Alpine', + 'systemd': 'Systemd', 'openrc': 'OpenRC', 'openbsd': 'OpenBSD', 'solaris': 'Solaris', 'freebsd': 'FreeBSD'} + class UnimplementedStrategy(object): def __init__(self, module): @@ -107,7 +116,12 @@ class Hostname(object): def __init__(self, module): self.module = module self.name = module.params['name'] - if self.platform == 'Linux' and ServiceMgrFactCollector.is_systemd_managed(module): + self.use = module.params['use'] + + if self.use is not None: + strat = globals()['%sStrategy' % STRATS[self.use]] + self.strategy = strat(module) + elif self.platform == 'Linux' and ServiceMgrFactCollector.is_systemd_managed(module): self.strategy = SystemdStrategy(module) else: self.strategy = self.strategy_class(module) @@ -179,7 +193,7 @@ class GenericStrategy(object): self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" % (rc, out, err)) def get_permanent_hostname(self): - return None + return 'UNKNOWN' def set_permanent_hostname(self, name): pass @@ -403,20 +417,22 @@ class OpenRCStrategy(GenericStrategy): HOSTNAME_FILE = '/etc/conf.d/hostname' def get_permanent_hostname(self): + name = 'UNKNOWN' try: try: f = open(self.HOSTNAME_FILE, 'r') for line in f: line = line.strip() if line.startswith('hostname='): - return line[10:].strip('"') + name = line[10:].strip('"') + break except Exception as e: self.module.fail_json(msg="failed to read hostname: %s" % to_native(e), exception=traceback.format_exc()) finally: f.close() - return None + return name def set_permanent_hostname(self, name): try: @@ -515,6 +531,7 @@ class FreeBSDStrategy(GenericStrategy): def get_permanent_hostname(self): + name = 'UNKNOWN' if not os.path.isfile(self.HOSTNAME_FILE): try: open(self.HOSTNAME_FILE, "a").write("hostname=temporarystub\n") @@ -527,14 +544,15 @@ class FreeBSDStrategy(GenericStrategy): for line in f: line = line.strip() if line.startswith('hostname='): - return line[10:].strip('"') + name = line[10:].strip('"') + break except Exception as e: self.module.fail_json(msg="failed to read hostname: %s" % to_native(e), exception=traceback.format_exc()) finally: f.close() - return None + return name def set_permanent_hostname(self, name): try: @@ -754,7 +772,8 @@ class NeonHostname(Hostname): def main(): module = AnsibleModule( argument_spec=dict( - name=dict(required=True) + name=dict(type='str', required=True), + use=dict(type='str', choices=STRATS.keys()) ), supports_check_mode=True, )