|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +DOCUMENTATION = ''' |
| 4 | +--- |
| 5 | +module: oracle_jcs_console_bootargs |
| 6 | +version_added: |
| 7 | +short_description: add or delete parameter in console weblogic |
| 8 | +description: |
| 9 | + - add boot paramters or delete with pattern to console weblogic |
| 10 | +options: |
| 11 | + state: |
| 12 | + description: |
| 13 | + - add or delete parameters |
| 14 | + choices: [ present, absent ] |
| 15 | + regexp: |
| 16 | + description: |
| 17 | + - the regex for delete in console |
| 18 | + type: str |
| 19 | + domainName: |
| 20 | + description: |
| 21 | + - the domain name of jcs |
| 22 | + wlst_path: |
| 23 | + description: |
| 24 | + - path of wlst script |
| 25 | + default: /u01/app/oracle/middleware/oracle_common/common/bin/wlst.sh |
| 26 | + domain_path: |
| 27 | + description: |
| 28 | + - path of home domain |
| 29 | + default: /u01/data/domains |
| 30 | + arguments: |
| 31 | + descripton: |
| 32 | + - args needed to add |
| 33 | + servers: |
| 34 | + description: |
| 35 | + - list of servers to add or delete boot args |
| 36 | +
|
| 37 | +requirements: |
| 38 | +- wlst |
| 39 | +
|
| 40 | +author: |
| 41 | + |
| 42 | +
|
| 43 | +''' |
| 44 | + |
| 45 | +EXAMPLES = ''' |
| 46 | +
|
| 47 | +- name: delete xmx and xms in boot args console |
| 48 | + oracle_jcs_console_rm_bootargs: |
| 49 | + state: absent |
| 50 | + regexp: '-Xm([sx])([0-9]+)m' |
| 51 | + domainName: DOMTSTAPI01 |
| 52 | + become: true |
| 53 | + become_user: oracle |
| 54 | +
|
| 55 | +- name: add xmx in boot args console |
| 56 | + oracle_jcs_console_rm_bootargs: |
| 57 | + state: present |
| 58 | + arguments: '-Xms256m -Xmx2048m' |
| 59 | + domainName: DOMTSTAPI01 |
| 60 | + become: true |
| 61 | + become_user: oracle |
| 62 | +
|
| 63 | +''' |
| 64 | + |
| 65 | + |
| 66 | +from ansible.module_utils.basic import * |
| 67 | + |
| 68 | + |
| 69 | +import os |
| 70 | +import tempfile |
| 71 | +import json |
| 72 | +import commands |
| 73 | +import types |
| 74 | +import ast |
| 75 | + |
| 76 | +def run(): |
| 77 | + module = AnsibleModule( |
| 78 | + argument_spec=dict( |
| 79 | + state = dict(required=True, type='str', choices=['absent', 'present']), |
| 80 | + arguments = dict(required=False, type='str'), |
| 81 | + regexp = dict(required=False, type='str'), |
| 82 | + domainName = dict(required=True, type='str'), |
| 83 | + wlst_path = dict(required=False, type='str', default='/u01/app/oracle/middleware/oracle_common/common/bin/wlst.sh'), |
| 84 | + domain_path = dict(required=False, type='str', default='/u01/data/domains'), |
| 85 | + servers = dict(required=False, type='list', default=['all']), |
| 86 | + servers_regexp = dict(required=False, type='str', default='') |
| 87 | + ), |
| 88 | + required_if=[ |
| 89 | + ["state", "absent", ["regexp"] ], |
| 90 | + ["state", "present", ["arguments"] ] |
| 91 | + ], |
| 92 | + supports_check_mode=True |
| 93 | + ) |
| 94 | + |
| 95 | + delete_args_py = """ |
| 96 | +import re |
| 97 | +import socket |
| 98 | +import os |
| 99 | +import commands |
| 100 | +import weblogic.security.internal.SerializedSystemIni |
| 101 | +import weblogic.security.internal.encryption.ClearOrEncryptedService |
| 102 | +from weblogic.security.internal import * |
| 103 | +from weblogic.security.internal.encryption import * |
| 104 | +
|
| 105 | +# Variables |
| 106 | +patternStr = "{regexp}" |
| 107 | +checkmode = {checkmode} |
| 108 | +
|
| 109 | +
|
| 110 | +try: |
| 111 | + cmd_grep_pass = 'grep -Po "(?<=<node-manager-password-encrypted>).*(?=</node-manager-password-encrypted>)" {domainpath}/{domainName}/config/config.xml > /tmp/pass_enc' |
| 112 | + os.system(cmd_grep_pass) |
| 113 | + password_enc = open('/tmp/pass_enc', 'r').read() |
| 114 | + os.remove('/tmp/pass_enc') |
| 115 | + secPath = '{domainpath}/{domainName}/security/' |
| 116 | + encService = SerializedSystemIni.getEncryptionService(secPath) |
| 117 | + coeService = ClearOrEncryptedService(encService) |
| 118 | + adminPassword = coeService.decrypt(str(password_enc)) |
| 119 | + adminurl = socket.gethostname().split('.', 1)[0][:-1] |
| 120 | +except Exception: |
| 121 | + msg='Error in decryptage of password, check {domainpath}/{domainName}/security' |
| 122 | + vars_dict = {{}} |
| 123 | + vars_dict['changed'] = 'error' |
| 124 | + vars_dict['msg'] = msg |
| 125 | + print vars_dict |
| 126 | + raise |
| 127 | +
|
| 128 | +try: |
| 129 | + connect('weblogic',adminPassword,'t3://' + adminurl + '1:7001') |
| 130 | +except Exception: |
| 131 | + msg='ERROR Can not connect to interface' |
| 132 | + vars_dict = {{}} |
| 133 | + vars_dict['changed'] = 'error' |
| 134 | + vars_dict['msg'] = msg |
| 135 | + print vars_dict |
| 136 | + raise |
| 137 | +
|
| 138 | +domainConfig() |
| 139 | +edit() |
| 140 | +startEdit() |
| 141 | +cd('/') |
| 142 | +servers_list = {servers} |
| 143 | +servers_regexp = '{servers_regexp}' |
| 144 | +
|
| 145 | +if 'all' in servers_list: |
| 146 | + srvList = cmo.getServers() |
| 147 | +else: |
| 148 | + srvList = servers_list |
| 149 | +
|
| 150 | +vars_dict= {{}} |
| 151 | +vars_dict['changed'] = False |
| 152 | +vars_dict['servers'] = {{}} |
| 153 | +
|
| 154 | +for curSrv in srvList: |
| 155 | + if 'all' in servers_list: |
| 156 | + if not servers_regexp == '': |
| 157 | + servername = curSrv.getName() |
| 158 | + if servers_regexp in servername: |
| 159 | + curSrvName = curSrv.getName() |
| 160 | + else: |
| 161 | + continue |
| 162 | + else: |
| 163 | + curSrvName = curSrv.getName() |
| 164 | + else: |
| 165 | + curSrvName = curSrv |
| 166 | + cd('/Servers/'+curSrvName+'/ServerStart/'+curSrvName) |
| 167 | + curStr = cmo.getArguments() |
| 168 | + vars_dict['servers'][curSrvName] = {{}} |
| 169 | + vars_dict['servers'][curSrvName]['old argument'] = curStr |
| 170 | + if curStr != None : |
| 171 | + if re.search(r''+patternStr,curStr): |
| 172 | + curStr = re.sub(r''+patternStr, "", curStr) |
| 173 | + if not checkmode: |
| 174 | + cmo.setArguments(curStr) |
| 175 | + vars_dict['changed'] = True |
| 176 | + vars_dict['servers'][curSrvName]['new argument'] = curStr |
| 177 | +save() |
| 178 | +activate() |
| 179 | +
|
| 180 | +
|
| 181 | +result = vars_dict |
| 182 | +print result |
| 183 | +exit |
| 184 | + """ |
| 185 | + add_args_py = """ |
| 186 | +import re |
| 187 | +import socket |
| 188 | +import os |
| 189 | +import commands |
| 190 | +import weblogic.security.internal.SerializedSystemIni |
| 191 | +import weblogic.security.internal.encryption.ClearOrEncryptedService |
| 192 | +from weblogic.security.internal import * |
| 193 | +from weblogic.security.internal.encryption import * |
| 194 | +
|
| 195 | +# Variables |
| 196 | +addargs = "{arguments}" |
| 197 | +checkmode = {checkmode} |
| 198 | +
|
| 199 | +try: |
| 200 | + cmd_grep_pass = 'grep -Po "(?<=<node-manager-password-encrypted>).*(?=</node-manager-password-encrypted>)" {domainpath}/{domainName}/config/config.xml > /tmp/pass_enc' |
| 201 | + os.system(cmd_grep_pass) |
| 202 | + password_enc = open('/tmp/pass_enc', 'r').read() |
| 203 | + os.remove('/tmp/pass_enc') |
| 204 | + secPath = '{domainpath}/{domainName}/security/' |
| 205 | + encService = SerializedSystemIni.getEncryptionService(secPath) |
| 206 | + coeService = ClearOrEncryptedService(encService) |
| 207 | + adminPassword = coeService.decrypt(str(password_enc)) |
| 208 | + adminurl = socket.gethostname().split('.', 1)[0][:-1] |
| 209 | +except Exception: |
| 210 | + msg='Error in decryptage of password, check {domainpath}/{domainName}/security' |
| 211 | + vars_dict = {{}} |
| 212 | + vars_dict['changed'] = 'error' |
| 213 | + vars_dict['msg'] = msg |
| 214 | + print vars_dict |
| 215 | + raise |
| 216 | +
|
| 217 | +try: |
| 218 | + connect('weblogic',adminPassword,'t3://' + adminurl + '1:7001') |
| 219 | +except Exception: |
| 220 | + msg='ERROR Can not connect to interface' |
| 221 | + vars_dict = {{}} |
| 222 | + vars_dict['changed'] = 'error' |
| 223 | + vars_dict['msg'] = msg |
| 224 | + print vars_dict |
| 225 | + raise |
| 226 | +
|
| 227 | +domainConfig() |
| 228 | +edit() |
| 229 | +startEdit() |
| 230 | +cd('/') |
| 231 | +servers_list = {servers} |
| 232 | +servers_regexp = '{servers_regexp}' |
| 233 | +if 'all' in servers_list: |
| 234 | + srvList = cmo.getServers() |
| 235 | +else: |
| 236 | + srvList = servers_list |
| 237 | +
|
| 238 | +vars_dict= {{}} |
| 239 | +vars_dict['changed'] = False |
| 240 | +vars_dict['servers'] = {{}} |
| 241 | +
|
| 242 | +for curSrv in srvList: |
| 243 | + if 'all' in servers_list: |
| 244 | + if not servers_regexp == '': |
| 245 | + servername = curSrv.getName() |
| 246 | + if servers_regexp in servername: |
| 247 | + curSrvName = curSrv.getName() |
| 248 | + else: |
| 249 | + continue |
| 250 | + else: |
| 251 | + curSrvName = curSrv.getName() |
| 252 | + else: |
| 253 | + curSrvName = curSrv |
| 254 | + cd('/Servers/'+curSrvName+'/ServerStart/'+curSrvName) |
| 255 | + curStr = cmo.getArguments() |
| 256 | + vars_dict['servers'][curSrvName] = {{}} |
| 257 | + vars_dict['servers'][curSrvName]['old argument'] = curStr |
| 258 | + if curStr == None : |
| 259 | + curStr = '' |
| 260 | + if not re.search(r''+addargs,curStr): |
| 261 | + curStr = addargs + ' ' + curStr |
| 262 | + if not checkmode: |
| 263 | + cmo.setArguments(curStr) |
| 264 | + vars_dict['changed'] = True |
| 265 | + vars_dict['servers'][curSrvName]['new argument'] = curStr |
| 266 | +save() |
| 267 | +activate() |
| 268 | +
|
| 269 | +
|
| 270 | +result = vars_dict |
| 271 | +print result |
| 272 | +exit |
| 273 | + """ |
| 274 | + tmpcreate = tempfile.NamedTemporaryFile() |
| 275 | + try: |
| 276 | + if module.params.get('state') == 'absent': |
| 277 | + 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)) |
| 278 | + if module.params.get('state') == 'present': |
| 279 | + 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)) |
| 280 | + except Exception: |
| 281 | + module.fail_json(msg='error appear in templating script py') |
| 282 | + tmpcreate.seek(0) |
| 283 | + tmp = tmpcreate.read() |
| 284 | + try: |
| 285 | + wls_create = module.params.get('wlst_path') + ' ' + tmpcreate.name+' 2>/dev/null|egrep -i "^ |^--|^{"' |
| 286 | + except Exception: |
| 287 | + module.fail_json(msg='error check path of wlst script') |
| 288 | + try: |
| 289 | + data_json = commands.getoutput(wls_create) |
| 290 | + data_json = ast.literal_eval(data_json) |
| 291 | + data = json.loads(json.dumps(data_json)) |
| 292 | + if data["changed"] == 'error': |
| 293 | + module.fail_json(msg=data["msg"]) |
| 294 | + else: |
| 295 | + if data["changed"]: |
| 296 | + changed=True |
| 297 | + else: |
| 298 | + changed=False |
| 299 | + module.exit_json(changed=changed, servers=data['servers']) |
| 300 | + except Exception: |
| 301 | + wlst_debug = module.params.get('wlst_path') + ' ' + tmpcreate.name |
| 302 | + msg_error = commands.getoutput(wlst_debug) |
| 303 | + msg = 'Could not parse json ! error is ' + msg_error |
| 304 | + module.fail_json(msg=msg) |
| 305 | + |
| 306 | +def main(): |
| 307 | + run() |
| 308 | +if __name__ == "__main__": |
| 309 | + main() |
| 310 | + |
0 commit comments