Merge pull request #1804 from saito-hideki/feature/hostname

Add Solaris support in hostname module
This commit is contained in:
Brian Coca 2015-07-25 11:13:29 -04:00
commit da39fd1625

View file

@ -21,7 +21,9 @@
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: hostname module: hostname
author: "Hiroaki Nakamura (@hnakamur)" author:
- "Hiroaki Nakamura (@hnakamur)"
- "Hideki Saito (@saito-hideki)"
version_added: "1.4" version_added: "1.4"
short_description: Manage hostname short_description: Manage hostname
requirements: [ hostname ] requirements: [ hostname ]
@ -116,13 +118,13 @@ class GenericStrategy(object):
- set_current_hostname(name) - set_current_hostname(name)
- set_permanent_hostname(name) - set_permanent_hostname(name)
""" """
def __init__(self, module): def __init__(self, module):
self.module = module self.module = module
self.hostname_cmd = self.module.get_bin_path('hostname', True)
HOSTNAME_CMD = '/bin/hostname'
def get_current_hostname(self): def get_current_hostname(self):
cmd = [self.HOSTNAME_CMD] cmd = [self.hostname_cmd]
rc, out, err = self.module.run_command(cmd) rc, out, err = self.module.run_command(cmd)
if rc != 0: if rc != 0:
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" % self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
@ -130,7 +132,7 @@ class GenericStrategy(object):
return out.strip() return out.strip()
def set_current_hostname(self, name): def set_current_hostname(self, name):
cmd = [self.HOSTNAME_CMD, name] cmd = [self.hostname_cmd, name]
rc, out, err = self.module.run_command(cmd) rc, out, err = self.module.run_command(cmd)
if rc != 0: if rc != 0:
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" % self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" %
@ -363,6 +365,39 @@ class OpenBSDStrategy(GenericStrategy):
# =========================================== # ===========================================
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'
cmd = [self.hostname_cmd, cmd_option, 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):
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):
cmd = [self.hostname_cmd, 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))
# ===========================================
class FedoraHostname(Hostname): class FedoraHostname(Hostname):
platform = 'Linux' platform = 'Linux'
distribution = 'Fedora' distribution = 'Fedora'
@ -486,6 +521,11 @@ class OpenBSDHostname(Hostname):
distribution = None distribution = None
strategy_class = OpenBSDStrategy strategy_class = OpenBSDStrategy
class SolarisHostname(Hostname):
platform = 'SunOS'
distribution = None
strategy_class = SolarisStrategy
# =========================================== # ===========================================
def main(): def main():