-
Notifications
You must be signed in to change notification settings - Fork 0
/
oracle_jcs_console_bootargs.py
310 lines (277 loc) · 9.33 KB
/
oracle_jcs_console_bootargs.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: oracle_jcs_console_bootargs
version_added:
short_description: add or delete parameter in console weblogic
description:
- add boot paramters or delete with pattern to console weblogic
options:
state:
description:
- add or delete parameters
choices: [ present, absent ]
regexp:
description:
- the regex for delete in console
type: str
domainName:
description:
- the domain name of jcs
wlst_path:
description:
- path of wlst script
default: /u01/app/oracle/middleware/oracle_common/common/bin/wlst.sh
domain_path:
description:
- path of home domain
default: /u01/data/domains
arguments:
descripton:
- args needed to add
servers:
description:
- list of servers to add or delete boot args
requirements:
- wlst
author:
'''
EXAMPLES = '''
- name: delete xmx and xms in boot args console
oracle_jcs_console_rm_bootargs:
state: absent
regexp: '-Xm([sx])([0-9]+)m'
domainName: DOMTSTAPI01
become: true
become_user: oracle
- name: add xmx in boot args console
oracle_jcs_console_rm_bootargs:
state: present
arguments: '-Xms256m -Xmx2048m'
domainName: DOMTSTAPI01
become: true
become_user: oracle
'''
from ansible.module_utils.basic import *
import os
import tempfile
import json
import commands
import types
import ast
def run():
module = AnsibleModule(
argument_spec=dict(
state = dict(required=True, type='str', choices=['absent', 'present']),
arguments = dict(required=False, type='str'),
regexp = dict(required=False, type='str'),
domainName = dict(required=True, type='str'),
wlst_path = dict(required=False, type='str', default='/u01/app/oracle/middleware/oracle_common/common/bin/wlst.sh'),
domain_path = dict(required=False, type='str', default='/u01/data/domains'),
servers = dict(required=False, type='list', default=['all']),
servers_regexp = dict(required=False, type='str', default='')
),
required_if=[
["state", "absent", ["regexp"] ],
["state", "present", ["arguments"] ]
],
supports_check_mode=True
)
delete_args_py = """
import re
import socket
import os
import commands
import weblogic.security.internal.SerializedSystemIni
import weblogic.security.internal.encryption.ClearOrEncryptedService
from weblogic.security.internal import *
from weblogic.security.internal.encryption import *
# Variables
patternStr = "{regexp}"
checkmode = {checkmode}
try:
cmd_grep_pass = 'grep -Po "(?<=<node-manager-password-encrypted>).*(?=</node-manager-password-encrypted>)" {domainpath}/{domainName}/config/config.xml > /tmp/pass_enc'
os.system(cmd_grep_pass)
password_enc = open('/tmp/pass_enc', 'r').read()
os.remove('/tmp/pass_enc')
secPath = '{domainpath}/{domainName}/security/'
encService = SerializedSystemIni.getEncryptionService(secPath)
coeService = ClearOrEncryptedService(encService)
adminPassword = coeService.decrypt(str(password_enc))
adminurl = socket.gethostname().split('.', 1)[0][:-1]
except Exception:
msg='Error in decryptage of password, check {domainpath}/{domainName}/security'
vars_dict = {{}}
vars_dict['changed'] = 'error'
vars_dict['msg'] = msg
print vars_dict
raise
try:
connect('weblogic',adminPassword,'t3://' + adminurl + '1:7001')
except Exception:
msg='ERROR Can not connect to interface'
vars_dict = {{}}
vars_dict['changed'] = 'error'
vars_dict['msg'] = msg
print vars_dict
raise
domainConfig()
edit()
startEdit()
cd('/')
servers_list = {servers}
servers_regexp = '{servers_regexp}'
if 'all' in servers_list:
srvList = cmo.getServers()
else:
srvList = servers_list
vars_dict= {{}}
vars_dict['changed'] = False
vars_dict['servers'] = {{}}
for curSrv in srvList:
if 'all' in servers_list:
if not servers_regexp == '':
servername = curSrv.getName()
if servers_regexp in servername:
curSrvName = curSrv.getName()
else:
continue
else:
curSrvName = curSrv.getName()
else:
curSrvName = curSrv
cd('/Servers/'+curSrvName+'/ServerStart/'+curSrvName)
curStr = cmo.getArguments()
vars_dict['servers'][curSrvName] = {{}}
vars_dict['servers'][curSrvName]['old argument'] = curStr
if curStr != None :
if re.search(r''+patternStr,curStr):
curStr = re.sub(r''+patternStr, "", curStr)
if not checkmode:
cmo.setArguments(curStr)
vars_dict['changed'] = True
vars_dict['servers'][curSrvName]['new argument'] = curStr
save()
activate()
result = vars_dict
print result
exit
"""
add_args_py = """
import re
import socket
import os
import commands
import weblogic.security.internal.SerializedSystemIni
import weblogic.security.internal.encryption.ClearOrEncryptedService
from weblogic.security.internal import *
from weblogic.security.internal.encryption import *
# Variables
addargs = "{arguments}"
checkmode = {checkmode}
try:
cmd_grep_pass = 'grep -Po "(?<=<node-manager-password-encrypted>).*(?=</node-manager-password-encrypted>)" {domainpath}/{domainName}/config/config.xml > /tmp/pass_enc'
os.system(cmd_grep_pass)
password_enc = open('/tmp/pass_enc', 'r').read()
os.remove('/tmp/pass_enc')
secPath = '{domainpath}/{domainName}/security/'
encService = SerializedSystemIni.getEncryptionService(secPath)
coeService = ClearOrEncryptedService(encService)
adminPassword = coeService.decrypt(str(password_enc))
adminurl = socket.gethostname().split('.', 1)[0][:-1]
except Exception:
msg='Error in decryptage of password, check {domainpath}/{domainName}/security'
vars_dict = {{}}
vars_dict['changed'] = 'error'
vars_dict['msg'] = msg
print vars_dict
raise
try:
connect('weblogic',adminPassword,'t3://' + adminurl + '1:7001')
except Exception:
msg='ERROR Can not connect to interface'
vars_dict = {{}}
vars_dict['changed'] = 'error'
vars_dict['msg'] = msg
print vars_dict
raise
domainConfig()
edit()
startEdit()
cd('/')
servers_list = {servers}
servers_regexp = '{servers_regexp}'
if 'all' in servers_list:
srvList = cmo.getServers()
else:
srvList = servers_list
vars_dict= {{}}
vars_dict['changed'] = False
vars_dict['servers'] = {{}}
for curSrv in srvList:
if 'all' in servers_list:
if not servers_regexp == '':
servername = curSrv.getName()
if servers_regexp in servername:
curSrvName = curSrv.getName()
else:
continue
else:
curSrvName = curSrv.getName()
else:
curSrvName = curSrv
cd('/Servers/'+curSrvName+'/ServerStart/'+curSrvName)
curStr = cmo.getArguments()
vars_dict['servers'][curSrvName] = {{}}
vars_dict['servers'][curSrvName]['old argument'] = curStr
if curStr == None :
curStr = ''
if not re.search(r''+addargs,curStr):
curStr = addargs + ' ' + curStr
if not checkmode:
cmo.setArguments(curStr)
vars_dict['changed'] = True
vars_dict['servers'][curSrvName]['new argument'] = curStr
save()
activate()
result = vars_dict
print result
exit
"""
tmpcreate = tempfile.NamedTemporaryFile()
try:
if module.params.get('state') == 'absent':
tmpcreate.write(delete_args_py.format(domainName=module.params.get('domainName'), domainpath=module.params.get('domain_path'), regexp=module.params.get('regexp'), servers=module.params.get('servers'), servers_regexp=module.params.get('servers_regexp'), checkmode=module.check_mode))
if module.params.get('state') == 'present':
tmpcreate.write(add_args_py.format(domainName=module.params.get('domainName'), domainpath=module.params.get('domain_path'), arguments=module.params.get('arguments'), servers=module.params.get('servers'), servers_regexp=module.params.get('servers_regexp'), checkmode=module.check_mode))
except Exception:
module.fail_json(msg='error appear in templating script py')
tmpcreate.seek(0)
tmp = tmpcreate.read()
try:
wls_create = module.params.get('wlst_path') + ' ' + tmpcreate.name+' 2>/dev/null|egrep -i "^ |^--|^{"'
except Exception:
module.fail_json(msg='error check path of wlst script')
try:
data_json = commands.getoutput(wls_create)
data_json = ast.literal_eval(data_json)
data = json.loads(json.dumps(data_json))
if data["changed"] == 'error':
module.fail_json(msg=data["msg"])
else:
if data["changed"]:
changed=True
else:
changed=False
module.exit_json(changed=changed, servers=data['servers'])
except Exception:
wlst_debug = module.params.get('wlst_path') + ' ' + tmpcreate.name
msg_error = commands.getoutput(wlst_debug)
msg = 'Could not parse json ! error is ' + msg_error
module.fail_json(msg=msg)
def main():
run()
if __name__ == "__main__":
main()