|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# Ansible interface to tl-config |
| 4 | +# |
| 5 | +# Copyright 2016- Karl Mikaelsson <derfian@cendio.se> for Cendio AB |
| 6 | + |
| 7 | +import subprocess |
| 8 | +from ansible.module_utils.basic import AnsibleModule |
| 9 | + |
| 10 | +DOCUMENTATION = ''' |
| 11 | +--- |
| 12 | +module: tlconfig |
| 13 | +short_description: Ansible interface to tl-config |
| 14 | +author: "Karl Mikaelsson, @derfian" |
| 15 | +requirements: |
| 16 | + - ThinLinc Server |
| 17 | +
|
| 18 | +''' |
| 19 | + |
| 20 | +EXAMPLES = ''' |
| 21 | +- tlconfig: param=/vsmserver/HA/enabled value=1 |
| 22 | +''' |
| 23 | + |
| 24 | +RETURN = ''' |
| 25 | +changed: |
| 26 | + type: bool |
| 27 | +param: |
| 28 | + type: str |
| 29 | +''' |
| 30 | + |
| 31 | +TLCONFIG = "/opt/thinlinc/bin/tl-config" |
| 32 | + |
| 33 | +def getvalue(param): |
| 34 | + tlproc = subprocess.Popen([TLCONFIG, "%s" % param], stdout=subprocess.PIPE) |
| 35 | + stdout, _ = tlproc.communicate() |
| 36 | + return stdout.rstrip() |
| 37 | + |
| 38 | +def setvalue(param, value): |
| 39 | + tlproc = subprocess.Popen([TLCONFIG, "%s=%s" % (param, value)]) |
| 40 | + return tlproc.wait() |
| 41 | + |
| 42 | +def main(): |
| 43 | + changed = False |
| 44 | + |
| 45 | + module = AnsibleModule(argument_spec=dict(param=dict(required=True, type='str'), |
| 46 | + value=dict(required=True)), |
| 47 | + supports_check_mode=True) |
| 48 | + |
| 49 | + if module.check_mode: |
| 50 | + module.exit_json(changed=module.params['value'] != getvalue(module.params['param'])) |
| 51 | + |
| 52 | + if len(module.params['param']) == 0: |
| 53 | + module.fail_json(msg="Param can't be empty") |
| 54 | + |
| 55 | + if len(module.params['value']) == 0: |
| 56 | + module.fail_json(msg="Value can't be empty") |
| 57 | + |
| 58 | + if getvalue(module.params['param']) != module.params['value']: |
| 59 | + ret = setvalue(module.params['param'], module.params['value']) |
| 60 | + |
| 61 | + if ret is not 0: |
| 62 | + msg = "Failed setting %s: tl-config returned %d" % (module.params['param'], ret) |
| 63 | + module.fail_json(msg=msg) |
| 64 | + changed = True |
| 65 | + |
| 66 | + module.exit_json(changed=changed, |
| 67 | + param=module.params['param'], |
| 68 | + value=module.params['value']) |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + main() |
0 commit comments