* Use state=present instead of state=load

* Mutual exclusion of 'path' and 'value'
This commit is contained in:
sheidan 2025-07-19 10:10:12 -04:00
commit 840fbf8c27

View file

@ -61,7 +61,7 @@ options:
description: description:
- Value to set for the specified dconf key. Value should be specified in GVariant format. Due to complexity of this - 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. format, it is best to have a look at existing values in the dconf database.
- Required for O(state=present). - Required for O(state=present). If provided, O(path) is not required.
- Although the type is specified as "raw", it should typically be specified as a string. However, boolean values in - 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 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"). is why the type of this parameter is "raw").
@ -70,13 +70,13 @@ options:
required: false required: false
description: description:
- Remote path to the configuration to apply. - Remote path to the configuration to apply.
- Required for O(state=load). - Required for O(state=present). If provided, O(value) is not required.
state: state:
type: str type: str
required: false required: false
default: present default: present
choices: ['read', 'load', 'present', 'absent'] choices: ['read', 'present', 'absent']
description: description:
- The action to take upon the key/value. - The action to take upon the key/value.
""" """
@ -134,7 +134,7 @@ EXAMPLES = r"""
community.general.dconf: community.general.dconf:
key: "/org/gnome/terminal/legacy/profiles/:" key: "/org/gnome/terminal/legacy/profiles/:"
path: "/tmp/solarized_dark.dump" path: "/tmp/solarized_dark.dump"
state: load state: present
""" """
@ -470,16 +470,15 @@ def main():
# Setup the Ansible module # Setup the Ansible module
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
state=dict(default='present', choices=['present', 'absent', 'read', 'load']), state=dict(default='present', choices=['present', 'absent', 'read']),
key=dict(required=True, type='str', no_log=False), key=dict(required=True, type='str', no_log=False),
# Converted to str below after special handling of bool. # Converted to str below after special handling of bool.
value=dict(required=False, default=None, type='raw'), value=dict(required=False, default=None, type='raw'),
path=dict(required=False, default=None, type='str'), path=dict(required=False, default=None, type='path'),
), ),
supports_check_mode=True, supports_check_mode=True,
required_if=[ required_if=[
('state', 'present', ['value']), ('state', 'present', ['value', 'path'], True),
('state', 'load', ['path']),
], ],
mutually_exclusive=[ mutually_exclusive=[
['value', 'path'] ['value', 'path']
@ -539,17 +538,27 @@ def main():
# Process based on different states. # Process based on different states.
if module.params['state'] == 'read': if module.params['state'] == 'read':
# TODO: Handle this case when 'state=present' and 'key' is the only one
value = dconf.read(module.params['key']) value = dconf.read(module.params['key'])
module.exit_json(changed=False, value=value) module.exit_json(changed=False, value=value)
elif module.params['state'] == 'present': elif module.params['state'] == 'present':
if module.params['path']:
# Use 'dconf load' to propagate multiple entries from the root given by 'key'
changed = dconf.load(module.params['key'], module.params['path'])
module.exit_json(changed=changed)
elif module.params['value']:
# Use 'dconf write' to modify the key
changed = dconf.write(module.params['key'], module.params['value']) changed = dconf.write(module.params['key'], module.params['value'])
module.exit_json(changed=changed) module.exit_json(changed=changed)
else:
# NOTE: This case shouldn't happen yet as 'key' and 'path' are
# required with 'state=present'
# TODO: if 'key' ends with '/' then 'dconf list' should be used
# else, 'dconf read'
module.fail_json(msg="'key' or 'path' must be defined.")
elif module.params['state'] == 'absent': elif module.params['state'] == 'absent':
changed = dconf.reset(module.params['key']) changed = dconf.reset(module.params['key'])
module.exit_json(changed=changed) module.exit_json(changed=changed)
elif module.params['state'] == 'load':
changed = dconf.load(module.params['key'], module.params['path'])
module.exit_json(changed=changed)
if __name__ == '__main__': if __name__ == '__main__':