From f5e8204dbc0a3573a12a119413c00134d0ecf80b Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Tue, 5 Jul 2016 15:43:05 +0200 Subject: [PATCH] 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) --- system/service.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/system/service.py b/system/service.py index 2e89fb87399..f49f74f421e 100644 --- a/system/service.py +++ b/system/service.py @@ -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):