2013-08-24 17:49:15 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Hiroaki Nakamura <hnakamur@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: hostname
|
2015-07-25 16:32:55 +02:00
|
|
|
author:
|
|
|
|
- "Hiroaki Nakamura (@hnakamur)"
|
|
|
|
- "Hideki Saito (@saito-hideki)"
|
2013-11-08 04:01:02 +01:00
|
|
|
version_added: "1.4"
|
2013-08-24 17:49:15 +02:00
|
|
|
short_description: Manage hostname
|
|
|
|
requirements: [ hostname ]
|
|
|
|
description:
|
|
|
|
- Set system's hostname
|
2014-07-28 15:19:41 +02:00
|
|
|
- Currently implemented on Debian, Ubuntu, Fedora, RedHat, openSUSE, Linaro, ScientificLinux, Arch, CentOS, AMI.
|
2014-12-26 06:02:36 +01:00
|
|
|
- Any distribution that uses systemd as their init system
|
2013-08-24 17:49:15 +02:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- Name of the host
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- hostname: name=web01
|
|
|
|
'''
|
|
|
|
|
2014-07-14 22:51:40 +02:00
|
|
|
from distutils.version import LooseVersion
|
|
|
|
|
2014-06-16 15:06:50 +02:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2014-06-14 19:29:04 +02:00
|
|
|
|
|
|
|
|
2013-08-25 00:32:37 +02:00
|
|
|
class UnimplementedStrategy(object):
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
|
|
|
|
def get_current_hostname(self):
|
|
|
|
self.unimplemented_error()
|
|
|
|
|
|
|
|
def set_current_hostname(self, name):
|
|
|
|
self.unimplemented_error()
|
|
|
|
|
|
|
|
def get_permanent_hostname(self):
|
|
|
|
self.unimplemented_error()
|
|
|
|
|
|
|
|
def set_permanent_hostname(self, name):
|
|
|
|
self.unimplemented_error()
|
|
|
|
|
|
|
|
def unimplemented_error(self):
|
|
|
|
platform = get_platform()
|
|
|
|
distribution = get_distribution()
|
2013-10-15 16:41:49 +02:00
|
|
|
if distribution is not None:
|
|
|
|
msg_platform = '%s (%s)' % (platform, distribution)
|
|
|
|
else:
|
|
|
|
msg_platform = platform
|
2013-08-25 00:32:37 +02:00
|
|
|
self.module.fail_json(
|
|
|
|
msg='hostname module cannot be used on platform %s' % msg_platform)
|
|
|
|
|
2013-08-24 17:49:15 +02:00
|
|
|
class Hostname(object):
|
|
|
|
"""
|
|
|
|
This is a generic Hostname manipulation class that is subclassed
|
|
|
|
based on platform.
|
|
|
|
|
|
|
|
A subclass may wish to set different strategy instance to self.strategy.
|
|
|
|
|
|
|
|
All subclasses MUST define platform and distribution (which may be None).
|
|
|
|
"""
|
|
|
|
|
|
|
|
platform = 'Generic'
|
|
|
|
distribution = None
|
2013-08-25 00:32:37 +02:00
|
|
|
strategy_class = UnimplementedStrategy
|
2013-08-24 17:49:15 +02:00
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
return load_platform_subclass(Hostname, args, kwargs)
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.name = module.params['name']
|
|
|
|
self.strategy = self.strategy_class(module)
|
|
|
|
|
|
|
|
def get_current_hostname(self):
|
|
|
|
return self.strategy.get_current_hostname()
|
|
|
|
|
|
|
|
def set_current_hostname(self, name):
|
|
|
|
self.strategy.set_current_hostname(name)
|
|
|
|
|
|
|
|
def get_permanent_hostname(self):
|
|
|
|
return self.strategy.get_permanent_hostname()
|
|
|
|
|
|
|
|
def set_permanent_hostname(self, name):
|
|
|
|
self.strategy.set_permanent_hostname(name)
|
|
|
|
|
|
|
|
class GenericStrategy(object):
|
|
|
|
"""
|
|
|
|
This is a generic Hostname manipulation strategy class.
|
|
|
|
|
|
|
|
A subclass may wish to override some or all of these methods.
|
|
|
|
- get_current_hostname()
|
|
|
|
- get_permanent_hostname()
|
|
|
|
- set_current_hostname(name)
|
|
|
|
- set_permanent_hostname(name)
|
|
|
|
"""
|
2015-07-25 16:32:55 +02:00
|
|
|
|
2013-08-24 17:49:15 +02:00
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
2015-07-25 16:32:55 +02:00
|
|
|
self.hostname_cmd = self.module.get_bin_path('hostname', True)
|
2013-08-24 17:49:15 +02:00
|
|
|
|
|
|
|
def get_current_hostname(self):
|
2015-07-25 16:32:55 +02:00
|
|
|
cmd = [self.hostname_cmd]
|
2013-08-25 00:35:20 +02:00
|
|
|
rc, out, err = self.module.run_command(cmd)
|
2013-08-24 17:49:15 +02:00
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
|
|
|
|
(rc, out, err))
|
|
|
|
return out.strip()
|
|
|
|
|
|
|
|
def set_current_hostname(self, name):
|
2015-07-25 16:32:55 +02:00
|
|
|
cmd = [self.hostname_cmd, name]
|
2013-08-25 00:35:20 +02:00
|
|
|
rc, out, err = self.module.run_command(cmd)
|
2013-08-24 17:49:15 +02:00
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
|
|
|
|
(rc, out, err))
|
|
|
|
|
|
|
|
def get_permanent_hostname(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def set_permanent_hostname(self, name):
|
|
|
|
pass
|
|
|
|
|
2014-06-14 19:29:04 +02:00
|
|
|
|
2013-08-24 17:49:15 +02:00
|
|
|
# ===========================================
|
|
|
|
|
|
|
|
class DebianStrategy(GenericStrategy):
|
|
|
|
"""
|
|
|
|
This is a Debian family Hostname manipulation strategy class - it edits
|
|
|
|
the /etc/hostname file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
HOSTNAME_FILE = '/etc/hostname'
|
|
|
|
|
|
|
|
def get_permanent_hostname(self):
|
2014-01-07 20:21:22 +01:00
|
|
|
if not os.path.isfile(self.HOSTNAME_FILE):
|
|
|
|
try:
|
|
|
|
open(self.HOSTNAME_FILE, "a").write("")
|
|
|
|
except IOError, err:
|
2014-06-12 12:06:59 +02:00
|
|
|
self.module.fail_json(msg="failed to write file: %s" %
|
2014-01-07 20:21:22 +01:00
|
|
|
str(err))
|
2013-08-24 17:49:15 +02:00
|
|
|
try:
|
2013-08-25 00:44:18 +02:00
|
|
|
f = open(self.HOSTNAME_FILE)
|
|
|
|
try:
|
2013-08-25 00:46:23 +02:00
|
|
|
return f.read().strip()
|
2013-08-25 00:44:18 +02:00
|
|
|
finally:
|
|
|
|
f.close()
|
2013-08-24 17:49:15 +02:00
|
|
|
except Exception, err:
|
|
|
|
self.module.fail_json(msg="failed to read hostname: %s" %
|
|
|
|
str(err))
|
|
|
|
|
|
|
|
def set_permanent_hostname(self, name):
|
|
|
|
try:
|
2013-08-25 00:44:18 +02:00
|
|
|
f = open(self.HOSTNAME_FILE, 'w+')
|
|
|
|
try:
|
2013-08-24 17:49:15 +02:00
|
|
|
f.write("%s\n" % name)
|
2013-08-25 00:44:18 +02:00
|
|
|
finally:
|
|
|
|
f.close()
|
2013-08-24 17:49:15 +02:00
|
|
|
except Exception, err:
|
|
|
|
self.module.fail_json(msg="failed to update hostname: %s" %
|
|
|
|
str(err))
|
|
|
|
|
2014-06-12 12:06:59 +02:00
|
|
|
|
2013-08-24 17:49:15 +02:00
|
|
|
# ===========================================
|
|
|
|
|
|
|
|
class RedHatStrategy(GenericStrategy):
|
|
|
|
"""
|
|
|
|
This is a Redhat Hostname strategy class - it edits the
|
|
|
|
/etc/sysconfig/network file.
|
|
|
|
"""
|
|
|
|
NETWORK_FILE = '/etc/sysconfig/network'
|
|
|
|
|
|
|
|
def get_permanent_hostname(self):
|
|
|
|
try:
|
2013-08-25 00:44:18 +02:00
|
|
|
f = open(self.NETWORK_FILE, 'rb')
|
|
|
|
try:
|
2013-08-24 17:49:15 +02:00
|
|
|
for line in f.readlines():
|
|
|
|
if line.startswith('HOSTNAME'):
|
|
|
|
k, v = line.split('=')
|
|
|
|
return v.strip()
|
2013-08-25 00:44:18 +02:00
|
|
|
finally:
|
|
|
|
f.close()
|
2013-08-24 17:49:15 +02:00
|
|
|
except Exception, err:
|
|
|
|
self.module.fail_json(msg="failed to read hostname: %s" %
|
|
|
|
str(err))
|
|
|
|
|
|
|
|
def set_permanent_hostname(self, name):
|
|
|
|
try:
|
|
|
|
lines = []
|
2013-12-08 03:10:42 +01:00
|
|
|
found = False
|
2013-08-25 00:44:18 +02:00
|
|
|
f = open(self.NETWORK_FILE, 'rb')
|
|
|
|
try:
|
2013-08-24 17:49:15 +02:00
|
|
|
for line in f.readlines():
|
|
|
|
if line.startswith('HOSTNAME'):
|
|
|
|
lines.append("HOSTNAME=%s\n" % name)
|
2013-12-08 03:10:42 +01:00
|
|
|
found = True
|
2013-08-24 17:49:15 +02:00
|
|
|
else:
|
|
|
|
lines.append(line)
|
2013-08-25 00:44:18 +02:00
|
|
|
finally:
|
|
|
|
f.close()
|
2013-12-08 03:10:42 +01:00
|
|
|
if not found:
|
|
|
|
lines.append("HOSTNAME=%s\n" % name)
|
2013-08-25 00:44:18 +02:00
|
|
|
f = open(self.NETWORK_FILE, 'w+')
|
|
|
|
try:
|
2013-08-24 17:49:15 +02:00
|
|
|
f.writelines(lines)
|
2013-08-25 00:44:18 +02:00
|
|
|
finally:
|
|
|
|
f.close()
|
2013-08-24 17:49:15 +02:00
|
|
|
except Exception, err:
|
|
|
|
self.module.fail_json(msg="failed to update hostname: %s" %
|
|
|
|
str(err))
|
|
|
|
|
2014-06-12 12:06:59 +02:00
|
|
|
|
2013-08-24 17:49:15 +02:00
|
|
|
# ===========================================
|
|
|
|
|
2014-12-26 06:02:36 +01:00
|
|
|
class SystemdStrategy(GenericStrategy):
|
2013-10-15 18:15:47 +02:00
|
|
|
"""
|
2014-12-26 06:02:36 +01:00
|
|
|
This is a Systemd hostname manipulation strategy class - it uses
|
2013-10-15 18:15:47 +02:00
|
|
|
the hostnamectl command.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_current_hostname(self):
|
|
|
|
cmd = ['hostname']
|
|
|
|
rc, out, err = self.module.run_command(cmd)
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
|
|
|
|
(rc, out, err))
|
|
|
|
return out.strip()
|
|
|
|
|
|
|
|
def set_current_hostname(self, name):
|
2015-05-19 04:55:51 +02:00
|
|
|
if len(name) > 64:
|
|
|
|
self.module.fail_json(msg="name cannot be longer than 64 characters on systemd servers, try a shorter name")
|
2013-10-15 18:15:47 +02:00
|
|
|
cmd = ['hostnamectl', '--transient', 'set-hostname', name]
|
|
|
|
rc, out, err = self.module.run_command(cmd)
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
|
|
|
|
(rc, out, err))
|
|
|
|
|
|
|
|
def get_permanent_hostname(self):
|
2014-01-24 15:31:45 +01:00
|
|
|
cmd = 'hostnamectl --static status'
|
2014-03-12 22:22:59 +01:00
|
|
|
rc, out, err = self.module.run_command(cmd, use_unsafe_shell=True)
|
2013-10-15 18:15:47 +02:00
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
|
|
|
|
(rc, out, err))
|
2014-07-28 15:22:10 +02:00
|
|
|
return out.strip()
|
2013-10-15 18:15:47 +02:00
|
|
|
|
|
|
|
def set_permanent_hostname(self, name):
|
2015-05-19 04:55:51 +02:00
|
|
|
if len(name) > 64:
|
|
|
|
self.module.fail_json(msg="name cannot be longer than 64 characters on systemd servers, try a shorter name")
|
2013-10-15 18:15:47 +02:00
|
|
|
cmd = ['hostnamectl', '--pretty', 'set-hostname', name]
|
|
|
|
rc, out, err = self.module.run_command(cmd)
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
|
|
|
|
(rc, out, err))
|
|
|
|
cmd = ['hostnamectl', '--static', 'set-hostname', name]
|
|
|
|
rc, out, err = self.module.run_command(cmd)
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
|
|
|
|
(rc, out, err))
|
|
|
|
|
2014-06-16 15:06:50 +02:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
|
2014-08-23 09:00:28 +02:00
|
|
|
class OpenRCStrategy(GenericStrategy):
|
|
|
|
"""
|
|
|
|
This is a Gentoo (OpenRC) Hostname manipulation strategy class - it edits
|
|
|
|
the /etc/conf.d/hostname file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
HOSTNAME_FILE = '/etc/conf.d/hostname'
|
|
|
|
|
|
|
|
def get_permanent_hostname(self):
|
|
|
|
try:
|
2014-12-16 15:49:13 +01:00
|
|
|
try:
|
|
|
|
f = open(self.HOSTNAME_FILE, 'r')
|
|
|
|
for line in f:
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith('hostname='):
|
|
|
|
return line[10:].strip('"')
|
|
|
|
except Exception, err:
|
|
|
|
self.module.fail_json(msg="failed to read hostname: %s" % str(err))
|
2014-11-08 18:08:47 +01:00
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
return None
|
2014-08-23 09:00:28 +02:00
|
|
|
|
|
|
|
def set_permanent_hostname(self, name):
|
|
|
|
try:
|
2014-12-16 15:49:13 +01:00
|
|
|
try:
|
|
|
|
f = open(self.HOSTNAME_FILE, 'r')
|
|
|
|
lines = [x.strip() for x in f]
|
2014-08-23 09:00:28 +02:00
|
|
|
|
2014-12-16 15:49:13 +01:00
|
|
|
for i, line in enumerate(lines):
|
|
|
|
if line.startswith('hostname='):
|
|
|
|
lines[i] = 'hostname="%s"' % name
|
|
|
|
break
|
|
|
|
f.close()
|
2014-08-23 09:00:28 +02:00
|
|
|
|
2014-12-16 15:49:13 +01:00
|
|
|
f = open(self.HOSTNAME_FILE, 'w')
|
|
|
|
f.write('\n'.join(lines) + '\n')
|
|
|
|
except Exception, err:
|
|
|
|
self.module.fail_json(msg="failed to update hostname: %s" % str(err))
|
2014-11-08 18:08:47 +01:00
|
|
|
finally:
|
|
|
|
f.close()
|
2014-08-23 09:00:28 +02:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
|
2015-05-18 05:42:49 +02:00
|
|
|
class OpenBSDStrategy(GenericStrategy):
|
|
|
|
"""
|
|
|
|
This is a OpenBSD family Hostname manipulation strategy class - it edits
|
|
|
|
the /etc/myname file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
HOSTNAME_FILE = '/etc/myname'
|
|
|
|
|
|
|
|
def get_permanent_hostname(self):
|
|
|
|
if not os.path.isfile(self.HOSTNAME_FILE):
|
|
|
|
try:
|
|
|
|
open(self.HOSTNAME_FILE, "a").write("")
|
|
|
|
except IOError, err:
|
|
|
|
self.module.fail_json(msg="failed to write file: %s" %
|
|
|
|
str(err))
|
|
|
|
try:
|
|
|
|
f = open(self.HOSTNAME_FILE)
|
|
|
|
try:
|
|
|
|
return f.read().strip()
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
except Exception, err:
|
|
|
|
self.module.fail_json(msg="failed to read hostname: %s" %
|
|
|
|
str(err))
|
|
|
|
|
|
|
|
def set_permanent_hostname(self, name):
|
|
|
|
try:
|
|
|
|
f = open(self.HOSTNAME_FILE, 'w+')
|
|
|
|
try:
|
|
|
|
f.write("%s\n" % name)
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
except Exception, err:
|
|
|
|
self.module.fail_json(msg="failed to update hostname: %s" %
|
|
|
|
str(err))
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
|
2015-07-25 14:48:13 +02:00
|
|
|
class SolarisStrategy(GenericStrategy):
|
|
|
|
"""
|
|
|
|
This is a Solaris11 or later Hostname manipulation strategy class - it
|
|
|
|
execute hostname command.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def set_current_hostname(self, name):
|
|
|
|
cmd_option = '-t'
|
2015-07-25 16:32:55 +02:00
|
|
|
cmd = [self.hostname_cmd, cmd_option, name]
|
2015-07-25 14:48:13 +02:00
|
|
|
rc, out, err = self.module.run_command(cmd)
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
|
|
|
|
(rc, out, err))
|
|
|
|
|
|
|
|
def get_permanent_hostname(self):
|
|
|
|
fmri = 'svc:/system/identity:node'
|
|
|
|
pattern = 'config/nodename'
|
|
|
|
cmd = '/usr/sbin/svccfg -s %s listprop -o value %s' % (fmri, pattern)
|
|
|
|
rc, out, err = self.module.run_command(cmd, use_unsafe_shell=True)
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
|
|
|
|
(rc, out, err))
|
|
|
|
return out.strip()
|
|
|
|
|
|
|
|
def set_permanent_hostname(self, name):
|
2015-07-25 16:32:55 +02:00
|
|
|
cmd = [self.hostname_cmd, name]
|
2015-07-25 14:48:13 +02:00
|
|
|
rc, out, err = self.module.run_command(cmd)
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
|
|
|
|
(rc, out, err))
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
|
2013-10-15 18:15:47 +02:00
|
|
|
class FedoraHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Fedora'
|
2014-12-26 06:02:36 +01:00
|
|
|
strategy_class = SystemdStrategy
|
2013-10-15 18:15:47 +02:00
|
|
|
|
2015-05-22 13:26:25 +02:00
|
|
|
class SLESHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Suse linux enterprise server '
|
|
|
|
distribution_version = get_distribution_version()
|
|
|
|
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("12"):
|
|
|
|
strategy_class = SystemdStrategy
|
|
|
|
else:
|
|
|
|
strategy_class = UnimplementedStrategy
|
|
|
|
|
2013-10-15 18:15:47 +02:00
|
|
|
class OpenSUSEHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Opensuse '
|
2014-12-26 06:02:36 +01:00
|
|
|
strategy_class = SystemdStrategy
|
2013-10-15 18:15:47 +02:00
|
|
|
|
|
|
|
class ArchHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Arch'
|
2014-12-26 06:02:36 +01:00
|
|
|
strategy_class = SystemdStrategy
|
2013-10-15 18:15:47 +02:00
|
|
|
|
2014-06-14 19:29:04 +02:00
|
|
|
class RedHat5Hostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Redhat'
|
|
|
|
strategy_class = RedHatStrategy
|
|
|
|
|
|
|
|
class RedHatServerHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Red hat enterprise linux server'
|
2014-12-14 21:54:30 +01:00
|
|
|
distribution_version = get_distribution_version()
|
2014-07-15 19:59:14 +02:00
|
|
|
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
|
2014-12-26 06:02:36 +01:00
|
|
|
strategy_class = SystemdStrategy
|
2014-06-14 19:29:04 +02:00
|
|
|
else:
|
|
|
|
strategy_class = RedHatStrategy
|
|
|
|
|
|
|
|
class RedHatWorkstationHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Red hat enterprise linux workstation'
|
2014-12-14 21:54:30 +01:00
|
|
|
distribution_version = get_distribution_version()
|
2014-07-15 19:59:14 +02:00
|
|
|
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
|
2014-12-26 06:02:36 +01:00
|
|
|
strategy_class = SystemdStrategy
|
2014-06-14 19:29:04 +02:00
|
|
|
else:
|
|
|
|
strategy_class = RedHatStrategy
|
|
|
|
|
|
|
|
class CentOSHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Centos'
|
2014-12-14 21:54:30 +01:00
|
|
|
distribution_version = get_distribution_version()
|
2014-09-16 18:53:13 +02:00
|
|
|
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
|
2014-12-26 06:02:36 +01:00
|
|
|
strategy_class = SystemdStrategy
|
2014-09-16 18:53:13 +02:00
|
|
|
else:
|
|
|
|
strategy_class = RedHatStrategy
|
2014-06-14 19:29:04 +02:00
|
|
|
|
2014-07-14 22:51:40 +02:00
|
|
|
class CentOSLinuxHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Centos linux'
|
2014-12-14 21:54:30 +01:00
|
|
|
distribution_version = get_distribution_version()
|
2014-09-16 18:53:13 +02:00
|
|
|
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
|
2014-12-26 06:02:36 +01:00
|
|
|
strategy_class = SystemdStrategy
|
2014-09-16 18:53:13 +02:00
|
|
|
else:
|
|
|
|
strategy_class = RedHatStrategy
|
2014-07-14 22:51:40 +02:00
|
|
|
|
|
|
|
class ScientificHostname(Hostname):
|
2014-06-14 19:29:04 +02:00
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Scientific'
|
2015-01-20 21:50:15 +01:00
|
|
|
distribution_version = get_distribution_version()
|
|
|
|
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
|
|
|
|
strategy_class = SystemdStrategy
|
|
|
|
else:
|
|
|
|
strategy_class = RedHatStrategy
|
2014-07-14 22:51:40 +02:00
|
|
|
|
|
|
|
class ScientificLinuxHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Scientific linux'
|
2015-01-20 21:50:15 +01:00
|
|
|
distribution_version = get_distribution_version()
|
|
|
|
if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
|
|
|
|
strategy_class = SystemdStrategy
|
|
|
|
else:
|
|
|
|
strategy_class = RedHatStrategy
|
2014-06-14 19:29:04 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-02-06 21:29:25 +01:00
|
|
|
class LinuxmintHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Linuxmint'
|
|
|
|
strategy_class = DebianStrategy
|
|
|
|
|
2014-06-14 19:29:04 +02:00
|
|
|
class LinaroHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Linaro'
|
|
|
|
strategy_class = DebianStrategy
|
|
|
|
|
2014-08-23 09:00:28 +02:00
|
|
|
class GentooHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Gentoo base system'
|
|
|
|
strategy_class = OpenRCStrategy
|
|
|
|
|
2015-04-24 20:25:14 +02:00
|
|
|
class ALTLinuxHostname(Hostname):
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = 'Altlinux'
|
|
|
|
strategy_class = RedHatStrategy
|
|
|
|
|
2015-05-18 05:42:49 +02:00
|
|
|
class OpenBSDHostname(Hostname):
|
|
|
|
platform = 'OpenBSD'
|
|
|
|
distribution = None
|
|
|
|
strategy_class = OpenBSDStrategy
|
|
|
|
|
2015-07-25 14:48:13 +02:00
|
|
|
class SolarisHostname(Hostname):
|
|
|
|
platform = 'SunOS'
|
|
|
|
distribution = None
|
|
|
|
strategy_class = SolarisStrategy
|
|
|
|
|
2013-10-15 18:15:47 +02:00
|
|
|
# ===========================================
|
|
|
|
|
2013-08-24 17:49:15 +02:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
name=dict(required=True, type='str')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
hostname = Hostname(module)
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
name = module.params['name']
|
|
|
|
current_name = hostname.get_current_hostname()
|
|
|
|
if current_name != name:
|
|
|
|
hostname.set_current_hostname(name)
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
permanent_name = hostname.get_permanent_hostname()
|
|
|
|
if permanent_name != name:
|
|
|
|
hostname.set_permanent_hostname(name)
|
|
|
|
changed = True
|
|
|
|
|
2014-09-30 10:13:54 +02:00
|
|
|
module.exit_json(changed=changed, name=name, ansible_facts=dict(ansible_hostname=name))
|
2013-08-24 17:49:15 +02:00
|
|
|
|
|
|
|
main()
|