mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-01 06:41:26 -07:00
Fix method exists in sysrc (#10005)
* Fix method exists. * Add changelog fragment. * Update the exists method to pass the present method tests. * Replace f-string formatting. * Update changelogs/fragments/10005-fix-method-exists-in-sysrc.yml Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * Add comment to the method exists. * Update plugins/modules/sysrc.py Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * Update changelogs/fragments/10005-fix-method-exists-in-sysrc.yml Co-authored-by: Felix Fontein <felix@fontein.de> * The improved comment formatting fixed. --------- Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
8910555983
commit
e0a283bb36
2 changed files with 16 additions and 12 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- sysrc - no longer always reporting ``changed=true`` when ``state=absent``. This fixes the method ``exists()`` (https://github.com/ansible-collections/community.general/issues/10004, https://github.com/ansible-collections/community.general/pull/10005).
|
|
@ -122,14 +122,19 @@ class Sysrc(object):
|
||||||
return err.find("unknown variable") > 0 or out.find("unknown variable") > 0
|
return err.find("unknown variable") > 0 or out.find("unknown variable") > 0
|
||||||
|
|
||||||
def exists(self):
|
def exists(self):
|
||||||
# sysrc doesn't really use exit codes
|
"""
|
||||||
(rc, out, err) = self.run_sysrc(self.name)
|
Tests whether the name is in the file. If parameter value is defined,
|
||||||
|
then tests whether name=value is in the file. These tests are necessary
|
||||||
|
because sysrc doesn't use exit codes. Instead, let sysrc read the
|
||||||
|
file's content and create a dictionary comprising the configuration.
|
||||||
|
Use this dictionary to preform the tests.
|
||||||
|
"""
|
||||||
|
(rc, out, err) = self.run_sysrc('-e', '-a')
|
||||||
|
conf = dict([i.split('=') for i in out.splitlines()])
|
||||||
if self.value is None:
|
if self.value is None:
|
||||||
regex = "%s: " % re.escape(self.name)
|
return self.name in conf
|
||||||
else:
|
else:
|
||||||
regex = "%s: %s$" % (re.escape(self.name), re.escape(self.value))
|
return self.name in conf and conf[self.name] == '"%s"' % self.value
|
||||||
|
|
||||||
return not self.has_unknown_variable(out, err) and re.match(regex, out) is not None
|
|
||||||
|
|
||||||
def contains(self):
|
def contains(self):
|
||||||
(rc, out, err) = self.run_sysrc('-n', self.name)
|
(rc, out, err) = self.run_sysrc('-n', self.name)
|
||||||
|
@ -142,13 +147,10 @@ class Sysrc(object):
|
||||||
if self.exists():
|
if self.exists():
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
self.changed = True
|
(rc, out, err) = self.run_sysrc("%s=%s" % (self.name, self.value))
|
||||||
return
|
|
||||||
|
|
||||||
(rc, out, err) = self.run_sysrc("%s=%s" % (self.name, self.value))
|
self.changed = True
|
||||||
if out.find("%s:" % self.name) == 0 and re.search("-> %s$" % re.escape(self.value), out) is not None:
|
|
||||||
self.changed = True
|
|
||||||
|
|
||||||
def absent(self):
|
def absent(self):
|
||||||
if not self.exists():
|
if not self.exists():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue