mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-05 05:34:22 -07:00
sysrc: fix Python 2.7 compatibility and set changed manually
This commit is contained in:
parent
93c75bfd6b
commit
09909adec6
1 changed files with 28 additions and 30 deletions
|
@ -103,7 +103,7 @@ changed:
|
||||||
sample: true
|
sample: true
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper, cause_changes
|
from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper
|
||||||
|
|
||||||
import errno
|
import errno
|
||||||
import os
|
import os
|
||||||
|
@ -130,11 +130,11 @@ class Sysrc(StateModuleHelper):
|
||||||
def _contains(self):
|
def _contains(self):
|
||||||
value = self._get()
|
value = self._get()
|
||||||
if value is None:
|
if value is None:
|
||||||
return (False, None)
|
return False, None
|
||||||
|
|
||||||
value = value.split(self.vars.delim)
|
value = value.split(self.vars.delim)
|
||||||
|
|
||||||
return (self.vars.value in value, value)
|
return self.vars.value in value, value
|
||||||
|
|
||||||
def _get(self):
|
def _get(self):
|
||||||
if not os.path.exists(self.vars.path):
|
if not os.path.exists(self.vars.path):
|
||||||
|
@ -151,10 +151,12 @@ class Sysrc(StateModuleHelper):
|
||||||
return out.strip()
|
return out.strip()
|
||||||
|
|
||||||
def _modify(self, op, changed):
|
def _modify(self, op, changed):
|
||||||
(rc, out, err) = self._sysrc(f"{self.vars.name}{op}={self.vars.delim}{self.vars.value}")
|
(rc, out, err) = self._sysrc("%s%s=%s%s" % (self.vars.name, op, self.vars.delim, self.vars.value))
|
||||||
if out.find(f"{self.vars.name}:") == 0:
|
if out.startswith("%s:" % self.vars.name):
|
||||||
return changed(out.split(' -> ')[1].strip().split(self.vars.delim))
|
return changed(out.split(' -> ')[1].strip().split(self.vars.delim))
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
def _sysrc(self, *args):
|
def _sysrc(self, *args):
|
||||||
cmd = [self.sysrc, '-f', self.vars.path]
|
cmd = [self.sysrc, '-f', self.vars.path]
|
||||||
if self.vars.jail:
|
if self.vars.jail:
|
||||||
|
@ -163,54 +165,50 @@ class Sysrc(StateModuleHelper):
|
||||||
|
|
||||||
(rc, out, err) = self.module.run_command(cmd)
|
(rc, out, err) = self.module.run_command(cmd)
|
||||||
if "Permission denied" in err:
|
if "Permission denied" in err:
|
||||||
raise OSError(errno.EACCES, "Permission denied for %s" % self.path)
|
raise OSError(errno.EACCES, "Permission denied for %s" % self.vars.path)
|
||||||
|
|
||||||
return (rc, out, err)
|
return rc, out, err
|
||||||
|
|
||||||
@cause_changes(when="success")
|
|
||||||
def state_absent(self):
|
def state_absent(self):
|
||||||
if self._get() is None:
|
if self._get() is None:
|
||||||
return False
|
return
|
||||||
|
|
||||||
if not self.check_mode:
|
if not self.check_mode:
|
||||||
self._sysrc('-x', self.vars.name)
|
self._sysrc('-x', self.vars.name)
|
||||||
|
|
||||||
return True
|
self.changed = True
|
||||||
|
|
||||||
@cause_changes(when="success")
|
|
||||||
def state_present(self):
|
def state_present(self):
|
||||||
value = self._get()
|
value = self._get()
|
||||||
if value == self.vars.value:
|
if value == self.vars.value:
|
||||||
return False
|
return
|
||||||
|
|
||||||
if self.vars.value is None:
|
if self.vars.value is None:
|
||||||
self.vars.set('value', value)
|
self.vars.set('value', value)
|
||||||
return False
|
return
|
||||||
|
|
||||||
if not self.check_mode:
|
if not self.check_mode:
|
||||||
self._sysrc(f"{self.name}={self.value}'")
|
self._sysrc("%s=%s" % (self.vars.name, self.vars.value))
|
||||||
|
|
||||||
return True
|
self.changed = True
|
||||||
|
|
||||||
@cause_changes(when="success")
|
|
||||||
def state_value_present(self):
|
|
||||||
(contains, value) = self._contains()
|
|
||||||
if contains:
|
|
||||||
return False
|
|
||||||
|
|
||||||
if self.vars.value is None:
|
|
||||||
self.vars.set('value', value)
|
|
||||||
return False
|
|
||||||
|
|
||||||
return self.check_mode or self._modify('+', lambda values: self.vars.value in values)
|
|
||||||
|
|
||||||
@cause_changes(when="success")
|
|
||||||
def state_value_absent(self):
|
def state_value_absent(self):
|
||||||
(contains, _unused) = self._contains()
|
(contains, _unused) = self._contains()
|
||||||
if not contains:
|
if not contains:
|
||||||
return False
|
return
|
||||||
|
|
||||||
return self.check_mode or self._modify('-', lambda values: self.vars.value not in values)
|
self.changed = self.check_mode or self._modify('-', lambda values: self.vars.value not in values)
|
||||||
|
|
||||||
|
def state_value_present(self):
|
||||||
|
(contains, value) = self._contains()
|
||||||
|
if contains:
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.vars.value is None:
|
||||||
|
self.vars.set('value', value)
|
||||||
|
return
|
||||||
|
|
||||||
|
self.changed = self.check_mode or self._modify('+', lambda values: self.vars.value in values)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue