Added 'use' option to hostname (#56679)
* Added 'use' option to hostname fixes #25543
This commit is contained in:
parent
b5ac9f5cf1
commit
4f78573a99
2 changed files with 28 additions and 7 deletions
2
changelogs/fragments/hostname_use.yml
Normal file
2
changelogs/fragments/hostname_use.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- added ``use`` option to ``hostname`` module to allow user to override autodetection.
|
|
@ -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,
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue