From 54243dc8f528f033c5b13ceadb1d75701035ed0e Mon Sep 17 00:00:00 2001 From: Hiroaki Nakamura Date: Wed, 16 Oct 2013 01:15:47 +0900 Subject: [PATCH] Add support for Fedora, OpenSUSE and ArchLinux. Tested under Fedora 19 and OpenSUSE 12.3. Not tested on ArchLinux. --- library/system/hostname | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/library/system/hostname b/library/system/hostname index 826e0f630d2..db814aad774 100644 --- a/library/system/hostname +++ b/library/system/hostname @@ -233,6 +233,64 @@ class CentOSHostname(Hostname): # =========================================== +class FedoraStrategy(GenericStrategy): + """ + This is a Fedora family Hostname manipulation strategy class - it uses + 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): + 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): + cmd = 'hostnamectl status | awk \'/^ *Static hostname:/{printf("%s", $3)}\'' + 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 + + def set_permanent_hostname(self, name): + 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)) + +class FedoraHostname(Hostname): + platform = 'Linux' + distribution = 'Fedora' + strategy_class = FedoraStrategy + +class OpenSUSEHostname(Hostname): + platform = 'Linux' + distribution = 'Opensuse ' + strategy_class = FedoraStrategy + +class ArchHostname(Hostname): + platform = 'Linux' + distribution = 'Arch' + strategy_class = FedoraStrategy + +# =========================================== + def main(): module = AnsibleModule( argument_spec = dict(