Repository: ansible/ansible-modules-core

Issue:
hostname module should allow hostnamectl on other distros with systemd as init
##### Issue Type:

Feature Idea
##### Component Name:

hostname
##### Ansible Version:

N/A
##### Summary:

The next version of both Debian and Ubuntu will provide systemd init systems by default, but still allows you to use their original init systems. So we need to make sure hostnamectl works only when systemd is available to be used.  Systemd is also optional on Gentoo, so we should check for it there too.

I submitted a PR to rename FedoraStrategy to SystemdStrategy already: https://github.com/ansible/ansible-modules-core/pull/572



Discussion:
also manjaro linux (an arch based distro) allows one to use openrc as the init system, so arch shouldn't be forced to systemd methods either.

at that point we will probably want to change from OS detection to tool detection

:+1: to using `hostnamectl` on Ubuntu.

I have created an ubuntu 14.04LTS box, and it has `hostnamectl` installed by default.

Is there a definitive list of versions that will support this?

Related to https://github.com/ansible/ansible-modules-core/issues/2308. (Edit -- oops wrong link!)

@hnakamur, @saito-hideki, ping. This issue is waiting on your response.
[click here for bot help](https://github.com/jctanner/ansibullbot/blob/ISSUE_TRIAGE_CLASS/ISSUE_HELP.md)

<!--- boilerplate: issue_notify_maintainer --->

Sorry I didn't notice this issue earlier.
Do you have an idea about how to check whether systemd is available to be used? Is it easy to check some particular file exists?

I am not so familiar with various OSes and distributions, so I don't know whether we should change from OS detection to tool detection or not.

If we use only tool detection, we must define the ordered list of which tool or config file to be checked for all OSes and distributions. I think this is hard to do it right.

So I think it is easier to use OS detection as base selection of strategy and additionally use tool detection for some OSes.

Could you tell me what do you think?

I asked about checking whether systemd is used at https://groups.google.com/d/msg/ansible-devel/CNJyrMRPp50/f0pmsG8SAgAJ


Resolving patch:
diff --git a/system/hostname.py b/system/hostname.py
--- a/system/hostname.py
+++ b/system/hostname.py
@@ -48,6 +48,7 @@
 
 # import module snippets
 from ansible.module_utils.basic import *
+from ansible.module_utils.facts import *
 
 
 class UnimplementedStrategy(object):
@@ -94,9 +95,12 @@ 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)
+        self.module       = module
+        self.name         = module.params['name']
+        if self.platform == 'Linux' and Facts(module).is_systemd_managed():
+            self.strategy = SystemdStrategy(module)
+        else:
+            self.strategy = self.strategy_class(module)
 
     def get_current_hostname(self):
         return self.strategy.get_current_hostname()
@@ -512,9 +516,7 @@ 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
-    elif distribution_version and LooseVersion("10") <= LooseVersion(distribution_version) <= LooseVersion("12"):
+    if distribution_version and LooseVersion("10") <= LooseVersion(distribution_version) <= LooseVersion("12"):
         strategy_class = SLESStrategy
     else:
         strategy_class = UnimplementedStrategy
@@ -537,65 +539,37 @@ class RedHat5Hostname(Hostname):
 class RedHatServerHostname(Hostname):
     platform = 'Linux'
     distribution = 'Red hat enterprise linux server'
-    distribution_version = get_distribution_version()
-    if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
-        strategy_class = SystemdStrategy
-    else:
-        strategy_class = RedHatStrategy
+    strategy_class = RedHatStrategy
 
 class RedHatWorkstationHostname(Hostname):
     platform = 'Linux'
     distribution = 'Red hat enterprise linux workstation'
-    distribution_version = get_distribution_version()
-    if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
-        strategy_class = SystemdStrategy
-    else:
-        strategy_class = RedHatStrategy
+    strategy_class = RedHatStrategy
 
 class CentOSHostname(Hostname):
     platform = 'Linux'
     distribution = 'Centos'
-    distribution_version = get_distribution_version()
-    if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
-        strategy_class = SystemdStrategy
-    else:
-        strategy_class = RedHatStrategy
+    strategy_class = RedHatStrategy
 
 class CentOSLinuxHostname(Hostname):
     platform = 'Linux'
     distribution = 'Centos linux'
-    distribution_version = get_distribution_version()
-    if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
-        strategy_class = SystemdStrategy
-    else:
-        strategy_class = RedHatStrategy
+    strategy_class = RedHatStrategy
 
 class ScientificHostname(Hostname):
     platform = 'Linux'
     distribution = 'Scientific'
-    distribution_version = get_distribution_version()
-    if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
-        strategy_class = SystemdStrategy
-    else:
-        strategy_class = RedHatStrategy
+    strategy_class = RedHatStrategy
 
 class ScientificLinuxHostname(Hostname):
     platform = 'Linux'
     distribution = 'Scientific linux'
-    distribution_version = get_distribution_version()
-    if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
-        strategy_class = SystemdStrategy
-    else:
-        strategy_class = RedHatStrategy
+    strategy_class = RedHatStrategy
 
 class OracleLinuxHostname(Hostname):
     platform = 'Linux'
     distribution = 'Oracle linux server'
-    distribution_version = get_distribution_version()
-    if distribution_version and LooseVersion(distribution_version) >= LooseVersion("7"):
-        strategy_class = SystemdStrategy
-    else:
-        strategy_class = RedHatStrategy
+    strategy_class = RedHatStrategy
 
 class AmazonLinuxHostname(Hostname):
     platform = 'Linux'
@@ -658,7 +632,7 @@ class FreeBSDHostname(Hostname):
 def main():
     module = AnsibleModule(
         argument_spec = dict(
-            name=dict(required=True, type='str')
+            name=dict(required=True)
         )
     )
 
@@ -682,4 +656,5 @@ def main():
                                         ansible_fqdn=socket.getfqdn(),
                                         ansible_domain='.'.join(socket.getfqdn().split('.')[1:])))
 
-main()
+if __name__ == '__main__':
+    main()


Test patch: