Skip to content

Instantly share code, notes, and snippets.

@tulir
Created February 4, 2017 16:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tulir/f513df91ef45c201da6d0de6b7a35aad to your computer and use it in GitHub Desktop.
Save tulir/f513df91ef45c201da6d0de6b7a35aad to your computer and use it in GitHub Desktop.
Ansible utilities for GNOME Shell configuration
#!/usr/bin/python
from ansible.module_utils.basic import *
def _set_value(module, path, value):
val = module.run_command(["dconf", "write", path, value])[1].strip()
def _get_value(module, path):
return module.run_command(["dconf", "read", path])[1].strip()
def _format(typ, value):
if typ == "bool":
value = str(value).lower()
elif typ == "int":
value = str(int(value))
elif typ == "float":
value = str(float(value))
elif typ == "string":
value = "'%s'" % value
elif typ == "array":
arr = value
value = ""
for item in arr:
value += "'%s', " % _format(item["type"], item["value"])
value = "[%s]" % value[:-2]
return value
def main():
module = AnsibleModule(
argument_spec={
"path": {
"required": True,
"type": "str"
},
"type": {
"type": "str",
"required": False,
},
"value": {
"required": True,
},
},
supports_check_mode=True)
path = module.params["path"]
value = module.params["value"]
old_value = _get_value(module, path)
value = _format(module.params["type"], value)
changed = old_value != str(value)
failed = False
if changed and not module.check_mode:
out = _set_value(module, path, value)
if type(out) == str and out.startswith("error: "):
failed = True
module.exit_json(
failed=failed,
changed=changed,
path=path,
value=value,
old_value=old_value)
main()
- name: GNOME Shell extension
gnome_shell_extension:
extension: "{{ extension_name }}"
state: present
- name: GNOME Shell config
dconf:
path: /org/gnome/{{ path_to_setting }}
value: "{{ value }}"
type: "{{ type }}"
#!/usr/bin/python
from ansible.module_utils.basic import *
def _enable(module, extension):
if module.check_mode:
return True
val = module.run_command(
["gnome-shell-extension-tool", "-e", extension])[1].strip()
# return val == "'%s' is now enabled." % extension or val == "'%s' is already enabled." % extension
return True
def _disable(module, extension):
if module.check_mode:
return True
val = module.run_command(
["gnome-shell-extension-tool", "-d", extension])[1].strip()
# return val == "'%s' is now disabled." % extension or val == "'%s' is not enabled or installed." % extension, val
return True
def _list(module):
extensions = module.run_command(
["dconf", "read", "/org/gnome/shell/enabled-extensions"])[1]
# Remove array braces
extensions = extensions[1:-1].split(", ")
# Remove quotes from values
return [extension[1:-1] for extension in extensions]
def main():
module = AnsibleModule(
argument_spec={
"extension": {
"required": True,
"type": "str"
},
"state": {
"required": True,
"type": "str",
"choices": ["present", "absent"]
},
},
supports_check_mode=True)
extension = module.params["extension"]
present = module.params["state"] == "present"
changed = False
failed = False
currentExts = _list(module)
if present:
if extension not in currentExts:
changed = True
failed = not _enable(module, extension)
else:
if extension in currentExts:
changed = True
failed = not _disable(module, extension)
module.exit_json(failed=failed, changed=changed)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment