Skip to content

Commit 5b6a8e7

Browse files
vbotkafelixfonteinrussoz
authored andcommitted
[stable-10] sysrc: Use shlex (#10400)
* Fix #10394 Use configparser. * Fix #10394 configparser is in Python3 only. * Fix #10394 return condition. * Fix #10394 Use ConfigParser with Python2. * Fix #10394 import configparser from ansible.module_utils.six.moves * Add changelog fragment. * Update changelogs/fragments/10400-sysrc.yml Co-authored-by: Felix Fontein <felix@fontein.de> * Fix #10394 use shlex instead of configparser. * Update fragment. * Update changelogs/fragments/10400-sysrc.yml Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/sysrc.py Co-authored-by: Felix Fontein <felix@fontein.de> * Apply suggestions from code review. Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> * Copy tests from #10417. --------- Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> (cherry picked from commit 0be6e61)
1 parent eab6f4c commit 5b6a8e7

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

changelogs/fragments/10400-sysrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- "sysrc - use ``shlex`` to improve parsing of ``sysrc -e -a`` output (https://github.com/ansible-collections/community.general/issues/10394, https://github.com/ansible-collections/community.general/pull/10400)."

plugins/modules/sysrc.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"""
104104

105105
from ansible.module_utils.basic import AnsibleModule
106+
from shlex import split
106107
import re
107108

108109

@@ -130,11 +131,12 @@ def exists(self):
130131
Use this dictionary to preform the tests.
131132
"""
132133
(rc, out, err) = self.run_sysrc('-e', '-a')
133-
conf = dict([i.split('=', 1) for i in out.splitlines()])
134+
conf = dict((part.split('=', 1) for part in split(out, comments=True)))
135+
134136
if self.value is None:
135137
return self.name in conf
136138
else:
137-
return self.name in conf and conf[self.name] == '"%s"' % self.value
139+
return conf.get(self.name) == self.value
138140

139141
def contains(self):
140142
(rc, out, err) = self.run_sysrc('-n', self.name)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) Ansible Project
2+
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3+
# SPDX-License-Identifier: GPL-3.0-or-later
4+
k1="v1"
5+
jail_list="
6+
foo
7+
bar"

tests/integration/targets/sysrc/tasks/main.yml

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,67 @@
369369
- "value_2 == sysrc_equals_sign_2.value"
370370
- "value_2 == conf.spamd_flags"
371371

372+
##
373+
## sysrc - #10004 state=absent when using default settings will report `changed=true`
374+
##
375+
- name: Test that a key from /etc/defaults/rc.conf is not used to mark changed
376+
sysrc:
377+
name: dumpdev
378+
state: absent
379+
path: /tmp/10004.conf
380+
register: sysrc_10004_absent
381+
382+
- name: Ensure that the defaults are not consulted
383+
assert:
384+
that:
385+
- not sysrc_10004_absent.changed
386+
387+
- name: Test that a delimited key from /etc/defaults/rc.conf is not used to mark changed
388+
sysrc:
389+
name: rc_conf_files
390+
state: value_absent
391+
path: /tmp/10004.conf
392+
register: sysrc_10004_value_absent
393+
394+
- name: Ensure that the default is not consulted
395+
assert:
396+
that:
397+
- not sysrc_10004_value_absent.changed
398+
399+
##
400+
## sysrc - #10394 Ensure that files with multi-line values work
401+
##
402+
- name: Copy 10394.conf
403+
copy:
404+
src: 10394.conf
405+
dest: /tmp/10394.conf
406+
407+
- name: Change value for k1
408+
sysrc:
409+
name: k1
410+
value: v2
411+
path: /tmp/10394.conf
412+
register: sysrc_10394_changed
413+
414+
- name: Get file content
415+
shell: "cat /tmp/10394.conf"
416+
register: sysrc_10394_content
417+
418+
- name: Ensure sysrc changed k1 from v1 to v2
419+
assert:
420+
that:
421+
- sysrc_10394_changed.changed
422+
- >
423+
'k1="v2"' in sysrc_10394_content.stdout_lines
424+
372425
always:
373426

374427
- name: Restore /etc/rc.conf
375428
copy:
376-
content: "{{ cached_etc_rcconf_content }}"
429+
content: "{{ cached_etc_rcconf_content.stdout }}"
377430
dest: /etc/rc.conf
378431

379432
- name: Restore /boot/loader.conf
380433
copy:
381-
content: "{{ cached_boot_loaderconf_content }}"
434+
content: "{{ cached_boot_loaderconf_content.stdout }}"
382435
dest: /boot/loader.conf

0 commit comments

Comments
 (0)