service module: use sysrc on FreeBSD (#4042)

* service module: use sysrc on FreeBSD

sysrc(8) is the designated userland program to edit rc files on FreeBSD.

It first appeared in FreeBSD 9.2, hence is available on all supported
versions of FreeBSD.

Side effect: fixes #2664

* Incorporate changes suggested by bcoca.

- Use `get_bin_path` to find sysrc binary.
- Only use sysrc when available (support for legacy versions of FreeBSD)
This commit is contained in:
Christian Schwarz 2016-07-05 15:43:05 +02:00 committed by Matt Clay
parent 186db40864
commit e25cb4df77

View file

@ -944,10 +944,11 @@ class FreeBsdService(Service):
def get_service_tools(self):
self.svc_cmd = self.module.get_bin_path('service', True)
if not self.svc_cmd:
self.module.fail_json(msg='unable to find service binary')
self.sysrc_cmd = self.module.get_bin_path('sysrc')
def get_service_status(self):
rc, stdout, stderr = self.execute_command("%s %s %s %s" % (self.svc_cmd, self.name, 'onestatus', self.arguments))
if self.name == "pf":
@ -992,10 +993,32 @@ class FreeBsdService(Service):
if self.rcconf_key is None:
self.module.fail_json(msg="unable to determine rcvar", stdout=stdout, stderr=stderr)
try:
return self.service_enable_rcconf()
except Exception:
self.module.fail_json(msg='unable to set rcvar')
if self.sysrc_cmd: # FreeBSD >= 9.2
rc, current_rcconf_value, stderr = self.execute_command("%s -n %s" % (self.sysrc_cmd, self.rcconf_key))
if rc != 0:
self.module.fail_json(msg="unable to get current rcvar value", stdout=stdout, stderr=stderr)
if current_rcconf_value.strip().upper() != self.rcconf_value:
self.changed = True
if self.module.check_mode:
self.module.exit_json(changed=True, msg="changing service enablement")
rc, stdout, stderr = self.execute_command("%s %s=\"%s\"" % (self.sysrc_cmd, self.rcconf_key, self.rcconf_value ) )
if rc != 0:
self.module.fail_json(msg="unable to set rcvar using sysrc", stdout=stdout, stderr=stderr)
else:
self.changed = False
else: # Legacy (FreeBSD < 9.2)
try:
return self.service_enable_rcconf()
except Exception:
self.module.fail_json(msg='unable to set rcvar')
def service_control(self):