-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathjbosscli.py
executable file
·211 lines (187 loc) · 5.98 KB
/
jbosscli.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python
#
# (c) 2017, Antonio Insusti <[email protected]>
#
# This file is part of Ansible
#
# JBoss Cli module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# JBoss Cli module is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# If not, see <http://www.gnu.org/licenses/>.
ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = """
author: "Antonio Insuasti (@wolfantEc)"
module: jbosscli
version_added: 2.4
short_description: deploy applications to JBoss
description:
- Deploy applications to JBoss standalone using the filesystem
options:
command:
required: false
description:
- JBoss Cli command
src:
required: false
description:
- File with list of commands
cli_path:
required: false
default: /usr/share/wildfly/bin
description:
- The location in the filesystem where jboss-cli.sh is located
user:
required: false
description:
- JBoss management user
password:
required: false
description:
- JBoss management user password
server:
required: false
default: localhost:9990
description:
- JBoss server or domain controller, with management port
verbose:
required: false
default: false
description:
- Show the JBoss Cli output, commonly in DMR
notes:
- "jboss-cli.sh need to be running on client host, and $JAVA_HOME/bin is needed in client $PATH"
- ""
"""
EXAMPLES = """
# Change scan-interval value, on user wildfly installation
- jbosscli:
command: /subsystem=deployment-scanner/scanner=default:write-attribute(name=scan-interval,value=6000)
cli_path: /home/user/wildfly-10.1.0.Final/bin
# Change ExampleDS datasource user-name, on 192.168.20.55:9990 default installation
- jbosscli:
command: /subsystem=datasources/data-source=ExampleDS:write-attribute(name=user-name,value=other)
server: 192.168.20.55:9990
# Undeploy the hello world application on wildfly server
- jbosscli:
command: undeploy hello.war
server: "{{ ansible_hostname}}:9990"
"""
RETURN = '''
---
changed:
description: if JBoss Cli command change something
returned: true
type: string
sample: 'changed: true'
command:
description: JBoss Cli command
returned: JBoss Cli command
type: string
sample: 'command: "/subsystem=deployment-scanner/scanner=default:write-attribute(name=scan-interval,value=1000)"'
stdout:
description: JBoss Cli command output
returned: If verbose JBoss Cli command output, else success or failed
type: string
sample: '{"outcome" => "success"}'
Error:
description: JBoss Cli command error
returned: return error if falied
type: string
sample: '{"outcome" => "success"}'
'''
import os
import re
import platform
from ansible.module_utils.basic import AnsibleModule
def main():
module = AnsibleModule(
argument_spec=dict(
src=dict(),
user=dict(),
password=dict(),
command=dict(),
cli_path=dict(default='/usr/share/wildfly/bin'),
server=dict(default='localhost:9990'),
verbose=dict(default="False"),
),
mutually_exclusive=[['command', 'src']],
)
src = module.params['src']
user = module.params['user']
password = module.params['password']
command = module.params['command']
cli_path = module.params['cli_path']
server = module.params['server']
verbose = module.params['verbose']
if user and not password:
module.fail_json(msg="Argument 'user' needs 'password' ")
if not os.access(cli_path + "/jboss-cli.sh", os.X_OK):
module.fail_json(msg="jboss-cli.sh in not found on cli_path ")
cmd = [cli_path + "/jboss-cli.sh"]
cmd.append('-c')
cmd.append("--controller=" + str(server))
if user:
cmd.append('--user=' + str(user))
cmd.append('--password=' + str(password))
if src:
cmd.append('--file=' + str(src))
else:
cmd.append('%s' % str(command))
rc = None
out = ''
err = ''
result = {}
result['command'] = command
(rc, out, err) = module.run_command(cmd, encoding='utf-8')
if rc is None:
result['changed'] = False
else:
result['changed'] = True
if rc != 0 and not out:
cause = re.findall("Caused.+", err)
if cause is not None:
module.fail_json(name='jboss-cli', msg=str(cause))
else:
module.fail_json(name='jboss-cli', msg=err)
if out and not src:
if not out.find("outcome") < 0:
if out.find('success'):
result['changed'] = True
if verbose == "True":
result['stdout'] = out
else:
result['stdout'] = 'success'
else:
result['changed'] = False
result['Error'] = re.findall("failure-description.+", out)
if verbose == "True":
result['stdout'] = out
else:
result['stdout'] = re.findall("outcome.+", out)
else:
result['changed'] = False
result['stdout'] = out
if src:
result['changed'] = True
result['stdout'] = out
if rc != 0:
failure = re.findall("failure-description.+", out)
if failure:
module.fail_json(name='jbosscli', msg=failure)
else:
module.fail_json(name='jbosscli', msg=out)
if err:
result['Error'] = err
module.exit_json(**result)
if __name__ == '__main__':
main()