Skip to content

Commit e0a283b

Browse files
vbotkarussozfelixfontein
authored
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 <[email protected]> * Add comment to the method exists. * Update plugins/modules/sysrc.py Co-authored-by: Alexei Znamensky <[email protected]> * Update changelogs/fragments/10005-fix-method-exists-in-sysrc.yml Co-authored-by: Felix Fontein <[email protected]> * The improved comment formatting fixed. --------- Co-authored-by: Alexei Znamensky <[email protected]> Co-authored-by: Felix Fontein <[email protected]>
1 parent 8910555 commit e0a283b

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- 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).

plugins/modules/sysrc.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,19 @@ def has_unknown_variable(self, out, err):
122122
return err.find("unknown variable") > 0 or out.find("unknown variable") > 0
123123

124124
def exists(self):
125-
# sysrc doesn't really use exit codes
126-
(rc, out, err) = self.run_sysrc(self.name)
125+
"""
126+
Tests whether the name is in the file. If parameter value is defined,
127+
then tests whether name=value is in the file. These tests are necessary
128+
because sysrc doesn't use exit codes. Instead, let sysrc read the
129+
file's content and create a dictionary comprising the configuration.
130+
Use this dictionary to preform the tests.
131+
"""
132+
(rc, out, err) = self.run_sysrc('-e', '-a')
133+
conf = dict([i.split('=') for i in out.splitlines()])
127134
if self.value is None:
128-
regex = "%s: " % re.escape(self.name)
135+
return self.name in conf
129136
else:
130-
regex = "%s: %s$" % (re.escape(self.name), re.escape(self.value))
131-
132-
return not self.has_unknown_variable(out, err) and re.match(regex, out) is not None
137+
return self.name in conf and conf[self.name] == '"%s"' % self.value
133138

134139
def contains(self):
135140
(rc, out, err) = self.run_sysrc('-n', self.name)
@@ -142,13 +147,10 @@ def present(self):
142147
if self.exists():
143148
return
144149

145-
if self.module.check_mode:
146-
self.changed = True
147-
return
150+
if not self.module.check_mode:
151+
(rc, out, err) = self.run_sysrc("%s=%s" % (self.name, self.value))
148152

149-
(rc, out, err) = self.run_sysrc("%s=%s" % (self.name, self.value))
150-
if out.find("%s:" % self.name) == 0 and re.search("-> %s$" % re.escape(self.value), out) is not None:
151-
self.changed = True
153+
self.changed = True
152154

153155
def absent(self):
154156
if not self.exists():

0 commit comments

Comments
 (0)