| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2016, Kenneth D. Evensen <[email protected]> |
| 4 |
# Copyright (c) 2017, Abhijeet Kasurde <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: gconftool2 |
| 12 |
author: |
| 13 |
- Kenneth D. Evensen (@kevensen) |
| 14 |
short_description: Edit GNOME Configurations |
| 15 |
description: |
| 16 |
- This module allows for the manipulation of GNOME 2 Configuration using C(gconftool-2). Please see the gconftool-2(1) man |
| 17 |
pages for more details. |
| 18 |
seealso: |
| 19 |
- name: C(gconftool-2) command manual page |
| 20 |
description: Manual page for the command. |
| 21 |
link: https://help.gnome.org/admin//system-admin-guide/2.32/gconf-6.html.en |
| 22 |
|
| 23 |
extends_documentation_fragment: |
| 24 |
- community.general._attributes |
| 25 |
attributes: |
| 26 |
check_mode: |
| 27 |
support: full |
| 28 |
diff_mode: |
| 29 |
support: none |
| 30 |
options: |
| 31 |
key: |
| 32 |
type: str |
| 33 |
description: |
| 34 |
- A GConf preference key is an element in the GConf repository that corresponds to an application preference. |
| 35 |
required: true |
| 36 |
value: |
| 37 |
type: str |
| 38 |
description: |
| 39 |
- Preference keys typically have simple values such as strings, integers, or lists of strings and integers. This is |
| 40 |
ignored unless O(state=present). |
| 41 |
value_type: |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- The type of value being set. This is ignored unless O(state=present). |
| 45 |
choices: [bool, float, int, string] |
| 46 |
state: |
| 47 |
type: str |
| 48 |
description: |
| 49 |
- The action to take upon the key/value. |
| 50 |
required: true |
| 51 |
choices: [absent, present] |
| 52 |
config_source: |
| 53 |
type: str |
| 54 |
description: |
| 55 |
- Specify a configuration source to use rather than the default path. |
| 56 |
direct: |
| 57 |
description: |
| 58 |
- Access the config database directly, bypassing server. If O(direct) is specified then the O(config_source) must be |
| 59 |
specified as well. |
| 60 |
type: bool |
| 61 |
default: false |
| 62 |
""" |
| 63 |
|
| 64 |
EXAMPLES = r""" |
| 65 |
- name: Change the widget font to "Serif 12" |
| 66 |
community.general.gconftool2: |
| 67 |
key: "/desktop/gnome/interface/font_name" |
| 68 |
value_type: "string" |
| 69 |
value: "Serif 12" |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = r""" |
| 73 |
key: |
| 74 |
description: The key specified in the module parameters. |
| 75 |
returned: success |
| 76 |
type: str |
| 77 |
sample: /desktop/gnome/interface/font_name |
| 78 |
value_type: |
| 79 |
description: The type of the value that was changed. |
| 80 |
returned: success |
| 81 |
type: str |
| 82 |
sample: string |
| 83 |
value: |
| 84 |
description: |
| 85 |
- The value of the preference key after executing the module or V(null) if key is removed. |
| 86 |
- From community.general 7.0.0 onwards it returns V(null) for a non-existent O(key), and returned V("") before that. |
| 87 |
returned: success |
| 88 |
type: str |
| 89 |
sample: "Serif 12" |
| 90 |
previous_value: |
| 91 |
description: |
| 92 |
- The value of the preference key before executing the module. |
| 93 |
- From community.general 7.0.0 onwards it returns V(null) for a non-existent O(key), and returned V("") before that. |
| 94 |
returned: success |
| 95 |
type: str |
| 96 |
sample: "Serif 12" |
| 97 |
version: |
| 98 |
description: Version of gconftool-2. |
| 99 |
type: str |
| 100 |
returned: always |
| 101 |
sample: "3.2.6" |
| 102 |
version_added: 10.0.0 |
| 103 |
""" |
| 104 |
|
| 105 |
from ansible_collections.community.general.plugins.module_utils._gconftool2 import gconftool2_runner |
| 106 |
from ansible_collections.community.general.plugins.module_utils._module_helper import StateModuleHelper |
| 107 |
|
| 108 |
|
| 109 |
class GConftool(StateModuleHelper): |
| 110 |
diff_params = ("value",) |
| 111 |
output_params = ("key", "value_type") |
| 112 |
facts_params = ("key", "value_type") |
| 113 |
facts_name = "gconftool2" |
| 114 |
module = dict( |
| 115 |
argument_spec=dict( |
| 116 |
key=dict(type="str", required=True, no_log=False), |
| 117 |
value_type=dict(type="str", choices=["bool", "float", "int", "string"]), |
| 118 |
value=dict(type="str"), |
| 119 |
state=dict(type="str", required=True, choices=["absent", "present"]), |
| 120 |
direct=dict(type="bool", default=False), |
| 121 |
config_source=dict(type="str"), |
| 122 |
), |
| 123 |
required_if=[ |
| 124 |
("state", "present", ["value", "value_type"]), |
| 125 |
("direct", True, ["config_source"]), |
| 126 |
], |
| 127 |
supports_check_mode=True, |
| 128 |
) |
| 129 |
|
| 130 |
def __init_module__(self): |
| 131 |
self.runner = gconftool2_runner(self.module, check_rc=True) |
| 132 |
if not self.vars.direct and self.vars.config_source is not None: |
| 133 |
self.do_raise('If the "config_source" is specified then "direct" must be "true"') |
| 134 |
|
| 135 |
with self.runner("version") as ctx: |
| 136 |
rc, out, err = ctx.run() |
| 137 |
self.vars.version = out.strip() |
| 138 |
|
| 139 |
self.vars.set("previous_value", self._get(), fact=True) |
| 140 |
self.vars.set("value_type", self.vars.value_type) |
| 141 |
self.vars.set("_value", self.vars.previous_value, output=False, change=True) |
| 142 |
self.vars.set_meta("value", initial_value=self.vars.previous_value) |
| 143 |
self.vars.set("playbook_value", self.vars.value, fact=True) |
| 144 |
|
| 145 |
def _make_process(self, fail_on_err): |
| 146 |
def process(rc, out, err): |
| 147 |
if err and fail_on_err: |
| 148 |
self.do_raise(f"gconftool-2 failed with error:\n{err.strip()}") |
| 149 |
out = out.rstrip() |
| 150 |
self.vars.value = None if out == "" else out |
| 151 |
return self.vars.value |
| 152 |
|
| 153 |
return process |
| 154 |
|
| 155 |
def _get(self): |
| 156 |
return self.runner("state key", output_process=self._make_process(False)).run(state="get") |
| 157 |
|
| 158 |
def state_absent(self): |
| 159 |
with self.runner("state key", output_process=self._make_process(False)) as ctx: |
| 160 |
ctx.run() |
| 161 |
self.vars.set("run_info", ctx.run_info, verbosity=4) |
| 162 |
self.vars.set("new_value", None, fact=True) |
| 163 |
self.vars._value = None |
| 164 |
|
| 165 |
def state_present(self): |
| 166 |
with self.runner( |
| 167 |
"direct config_source value_type state key value", output_process=self._make_process(True) |
| 168 |
) as ctx: |
| 169 |
ctx.run() |
| 170 |
self.vars.set("run_info", ctx.run_info, verbosity=4) |
| 171 |
self.vars.set("new_value", self._get(), fact=True) |
| 172 |
self.vars._value = self.vars.new_value |
| 173 |
|
| 174 |
|
| 175 |
def main(): |
| 176 |
GConftool.execute() |
| 177 |
|
| 178 |
|
| 179 |
if __name__ == "__main__": |
| 180 |
main() |