forked from ap1kenobi/ansible-winrm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winrm.py
83 lines (68 loc) · 2.39 KB
/
winrm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
def main():
tclwinrmrunnerpath = ""
tclpath = ""
operationtimeout = ""
debugmode = ""
module = AnsibleModule(
argument_spec = dict(
psscriptname=dict(required=True),
pshostname=dict(required=True),
psusername=dict(required=True),
pspassword=dict(required=True),
tclwinrmrunnerpath=dict(required=False),
tclpath=dict(required=False),
operationtimeout=dict(required=False),
debugmode=dict(required=False),
),
supports_check_mode=False,
)
if not tclwinrmrunnerpath:
tclwinrmrunnerpath = '/opt/tclwinrunner/winrmrunner.tcl'
if not tclpath:
tclpath = '/opt/ActiveTcl-8.6/bin/tclsh'
if not operationtimeout:
operationtimeout = '120'
if not debugmode:
debugmode = '0'
psscriptname = module.params['psscriptname']
pshostname = module.params['pshostname']
psusername = module.params['psusername']
pspassword = module.params['pspassword']
operationtimeout = module.params['operationtimeout']
debugmode = module.params['debugmode']
proc = subprocess.Popen([tclpath, tclwinrmrunnerpath, psscriptname,pshostname, psusername, pspassword], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
if err:
#We have an error
module.fail_json(msg=err)
outarray = out.splitlines()
AnsibleError = None
AnsibleResult = None
AnsibleDetail = None
for line in outarray:
if "AnsibleResult" in line:
AnsibleResult = line
AnsibleResult = AnsibleResult.replace("AnsibleResult:","")
if "AnsibleDetail" in line:
AnsibleDetail = line
AnsibleDetail = AnsibleDetail.replace("AnsibleDetail:","")
if "AnsibleError" in line:
AnsibleError = line
AnsibleError = AnsibleError.replace("AnsibleError:","")
if AnsibleError:
#We have a script-based error
module.fail_json(msg="Script-based error: " + AnsibleError)
#No error
if AnsibleResult == "Changed":
changed=True
elif AnsibleResult == "Unchanged":
changed=False
else:
changed=True
module.exit_json(changed=changed,msg=out)
# import module snippets
from ansible.module_utils.basic import *
main()