Skip to content

Commit 9918f10

Browse files
Thulium-DrakeCendioOssman
authored andcommitted
Added support for diff mode in tlconfig
1 parent 61673ec commit 9918f10

1 file changed

Lines changed: 28 additions & 17 deletions

File tree

library/tlconfig.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,52 @@
3131
TLCONFIG = "/opt/thinlinc/bin/tl-config"
3232

3333
def getvalue(param):
34-
tlproc = subprocess.Popen([TLCONFIG, "%s" % param], stdout=subprocess.PIPE,universal_newlines=True)
35-
stdout, _ = tlproc.communicate()
36-
return stdout.rstrip()
34+
tlproc = subprocess.run([TLCONFIG, "%s" % param], stdout=subprocess.PIPE, universal_newlines=True)
35+
return tlproc.stdout.strip("\n")
3736

38-
def setvalue(param, value):
39-
tlproc = subprocess.Popen([TLCONFIG, "%s=%s" % (param, value)])
40-
return tlproc.wait()
4137

42-
def main():
43-
changed = False
38+
def setvalue(param, value, check_mode):
39+
if not check_mode:
40+
tlproc = subprocess.run([TLCONFIG, "%s=%s" % (param, value)])
41+
return tlproc.returncode
42+
else:
43+
return 0
4444

45+
def main():
4546
module = AnsibleModule(argument_spec=dict(param=dict(required=True, type='str'),
4647
value=dict(required=True)),
4748
supports_check_mode=True)
4849

49-
if module.check_mode:
50-
module.exit_json(changed=module.params['value'] != getvalue(module.params['param']))
50+
result = {
51+
"changed": False,
52+
"param": module.params['param'],
53+
"value": module.params['value']
54+
}
5155

5256
if len(module.params['param']) == 0:
5357
module.fail_json(msg="Param can't be empty")
5458

5559
if len(module.params['value']) == 0:
5660
module.fail_json(msg="Value can't be empty")
5761

58-
if getvalue(module.params['param']) != module.params['value']:
59-
ret = setvalue(module.params['param'], module.params['value'])
62+
current = getvalue(module.params['param'])
63+
new = module.params['value']
6064

61-
if ret is not 0:
65+
if current != new:
66+
ret = setvalue(module.params['param'], module.params['value'], module.check_mode)
67+
68+
if ret != 0:
6269
msg = "Failed setting %s: tl-config returned %d" % (module.params['param'], ret)
6370
module.fail_json(msg=msg)
64-
changed = True
71+
result['changed'] = True
72+
73+
if module._diff:
74+
result['diff'] = {
75+
"before": current,
76+
"after": new
77+
}
6578

66-
module.exit_json(changed=changed,
67-
param=module.params['param'],
68-
value=module.params['value'])
79+
module.exit_json(**result)
6980

7081
if __name__ == '__main__':
7182
main()

0 commit comments

Comments
 (0)