Skip to content

Instantly share code, notes, and snippets.

@tulir tulir/apm.py
Created Jan 9, 2017

Embed
What would you like to do?
Ansible modules
#!/usr/bin/python
import subprocess
from ansible.module_utils.basic import *
def get_installed(links=False):
packages = {}
output = subprocess.check_output([
'apm',
'list',
'--links={}'.format(links),
'-i',
'-b',
]).strip().split('\n')
for line in output:
if '@' in line:
name, version = line.split('@')
packages[name] = version
return packages
def install_package(name, version):
if version is not None:
install_name = '{}@{}'.format(name, version)
else:
install_name = name
subprocess.check_output([
'apm',
'install',
install_name,
])
def main():
module = AnsibleModule(
argument_spec={
'name': {
'required': True
},
'version': {
'default': None
},
}, )
name = module.params['name']
version = module.params['version']
changed = False
try:
packages = get_installed()
if name not in packages or (version is not None and
version != packages[name]):
changed = True
install_package(name, version)
except subprocess.CalledProcessError as e:
module.fail_json(msg=e.output)
else:
module.exit_json(changed=changed)
if __name__ == '__main__':
main()
#!/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()
#!/usr/bin/python
######################
# WARNING: UNTESTED! #
######################
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)
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)
def _list(module):
extensions = module.run_command(
["dconf", "read", "/org/gnome/shell/enabled-extensions"])
# 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 extension in currentExts:
if not present:
changed = True
failed = not _enable(module, extension)
else:
if present:
changed = True
failed = not _disable(module, extension)
module.exit_json(
failed=failed,
changed=changed,
path=path,
value=value,
old_value=old_value)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.