Skip to content

Commit

Permalink
dconf: Convert boolean values into the format that dconf expects
Browse files Browse the repository at this point in the history
Even though we warn users to be careful to specify GVariant strings
for values, a common error is to be trying to specify a boolean string
which ends up getting converted into a boolean by the YAML parser or
Ansible. Then it gets converted to "True" or "False", the string
representations of Python booleans, which are not valid GVariants.

Rather than just failing with an obscure error when this happens,
let's be more user-friendly and detect when the user has specified a
boolean and convert it into the correct GVariant forms, "true" or
"false", so it just works. There's no good reason to be more pedantic
than that.
  • Loading branch information
jikamens committed Mar 30, 2023
1 parent d573cfc commit a37589c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/6206-dconf-booleans.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "dconf - be forgiving about boolean values: convert them to GVariant booleans automatically (https:/ansible-collections/community.general/pull/6206)."
23 changes: 21 additions & 2 deletions plugins/modules/dconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@
description:
- A dconf key to modify or read from the dconf database.
value:
type: str
type: raw
required: false
description:
- Value to set for the specified dconf key. Value should be specified in
GVariant format. Due to complexity of this format, it is best to have a
look at existing values in the dconf database.
- Required for I(state=present).
- Although the type is specified as "raw", it should typically be
specified as a string. However, boolean values in particular are
handled properly even when specified as booleans rather than strings
(in fact, handling booleans properly is why the type of this parameter
is "raw").
state:
type: str
required: false
Expand Down Expand Up @@ -155,6 +160,7 @@
HAS_PSUTIL = False

from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.common.text.converters import to_native


class DBusWrapper(object):
Expand Down Expand Up @@ -405,11 +411,24 @@ def main():
argument_spec=dict(
state=dict(default='present', choices=['present', 'absent', 'read']),
key=dict(required=True, type='str', no_log=False),
value=dict(required=False, default=None, type='str'),
# Converted to str below after special handling of bool.
value=dict(required=False, default=None, type='raw'),
),
supports_check_mode=True
)

# Try to be forgiving about the user specifying a boolean as the value, or
# more accurately about the fact that YAML and Ansible are quite insistent
# about converting strings that look like booleans into booleans. Convert
# the boolean into a string of the type dconf will understand. Any type for
# the value other than boolean is just converted into a string directly.
if module.params['value'] is not None:
if isinstance(module.params['value'], bool):
module.params['value'] = 'true' if module.params['value'] else 'false'
else:
module.params['value'] = to_native(
module.params['value'], errors='surrogate_or_strict')

if Variant is None:
module.warn(
'WARNING: The gi.repository Python library is not available; '
Expand Down

0 comments on commit a37589c

Please sign in to comment.