forked from duhlig/ansible-oracle-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oracle_db
1008 lines (906 loc) · 33 KB
/
oracle_db
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: oracle_db
short_description: Manage an Oracle database
description:
- Create/delete a database using dbca
- If a responsefile is available, that will be used. If initparams is defined, those will be attached to the createDatabase command
- If no responsefile is created, the database will be created based on all other parameters
version_added: "2.4.0.0"
options:
oracle_home:
description:
- The home where the database will be created
required: False
aliases: ['oh']
db_name:
description:
- The name of the database
required: True
default: None
aliases: ['db','database_name','name']
sid:
description:
- The instance name
required: False
default: None
db_unique_name:
description:
- The database db_unique_name
required: False
default: None
aliases: ['dbunqn','unique_name']
sys_password:
description:
- Password for the sys user
required: False
default: None
aliases: ['syspw','sysdbapassword','sysdbapw']
system_password:
description:
- Password for the system user
- If not set, defaults to sys_password
required: False
default: None
aliases: ['systempw']
dbsnmp_password:
description:
- Password for the dbsnmp user
- If not set, defaults to sys_password
required: False
default: None
aliases: ['dbsnmppw']
responsefile:
description:
- The name of responsefile
required: True
default: None
template:
description:
- The template the database will be based off
required: False
default: General_Purpose.dbc
cdb:
description:
- Should the database be a container database
required: False
default: False
aliases: ['container']
choices: ['True','False']
datafile_dest:
description:
- Where the database files should be placed (ASM diskgroup or filesystem path)
required: False
default: False
aliases: ['dfd']
recoveryfile_dest:
description:
- Where the database files should be placed (ASM diskgroup or filesystem path)
required: False
default: False
aliases: ['rfd']
storage_type:
description:
- Type of underlying storage (Filesystem or ASM)
required: False
default: FS
aliases: ['storage']
choices: ['FS','ASM']
dbconfig_type:
description:
- Type of database (SI,RAC,RON)
required: False
default: SI
choices: ['SI','RAC','RACONENODE']
db_type:
description:
- Default Type of database (MULTIPURPOSE, OLTP, DATA_WAREHOUSING)
required: False
default: MULTIPURPOSE
choices: ['MULTIPURPOSE','OLTP','DATA_WAREHOUSING']
racone_service:
description:
- If dbconfig_type = RACONENODE, a service has to be created along with the DB. This is the name of that service
- If no name is defined, the service will be called "{{ db_name }}_ronserv"
required: False
default: None
aliases: ['ron_service']
characterset:
description:
- The database characterset
required: False
default: AL32UTF8
memory_percentage:
description:
- The database total memory in % of available memory
required: False
memory_totalmb:
description:
- The database total memory in MB. Defaults to 1G
required: False
default: ['1024']
nodelist:
description:
- The list of nodes a RAC DB should be created on
required: False
amm:
description:
- Should Automatic Memory Management be used (memory_target, memory_max_target)
required: False
Default: False
choices: ['True','False']
initparams:
description:
- List of key=value pairs
- e.g
init_params:
- sga_target=1G
- sga_max_size=1G
required: False
customscripts:
description:
- List of scripts to run after database is created
- e.g
customScripts:
- /tmp/xxx.sql
- /tmp/yyy.sql
required: False
default_tablespace_type:
description:
- Database default tablespace type (DEFAULT_TBS_TYPE)
default: smallfile
choices: ['smallfile','bigfile']
default_tablespace:
description:
- Database default tablespace
default: smallfile
required: False
default_temp_tablespace:
description:
- Database default temporary tablespace
required: False
archivelog:
description:
- Puts the database is archivelog mode
required: False
default: false
choices: ['True','False']
type: bool
force_logging:
description:
- Enables force logging for the Database
required: False
default: false
choices: ['True','False']
type: bool
flashback:
description:
- Enables flashback for the database
required: False
default: false
choices: ['True','False']
type: bool
state:
description:
- The intended state of the database
default: present
choices: ['present','absent']
hostname:
description:
- The host of the database if using dbms_service
required: false
default: localhost
aliases: ['host']
port:
description:
- The listener port to connect to the database if using dbms_service
required: false
default: 1521
notes:
- cx_Oracle needs to be installed
requirements: [ "cx_Oracle" ]
author: Mikael Sandström, oravirt@gmail.com, @oravirt
'''
EXAMPLES = '''
# Create a DB (non-cdb)
oracle_db:
oh=/u01/app/oracle/12.2.0.1/db1
db_name=orclcdb
syspw=Oracle_123
state=present
storage=ASM
dfd=+DATA
rfd=+DATA
default_tablespace_type: bigfile
- hosts: all
gather_facts: true
vars:
oracle_home: /u01/app/oracle/12.2.0.1/db1
dbname: orclcdb
dbunqname: "{{ dbname}}_unq"
container: True
dbsid: "{{ dbname }}"
hostname: "{{ ansible_hostname }}"
oracle_env:
ORACLE_HOME: "{{ oracle_home }}"
LD_LIBRARY_PATH: "{{ oracle_home }}/lib"
myaction: present
rspfile: "/tmp/dbca_{{dbname}}.rsp"
initparameters:
- memory_target=0
- memory_max_target=0
- sga_target=1500M
- sga_max_size=1500M
dfd: +DATA
rfd: +FRA
storage: ASM
dbtype: SI
#ron_service: my_ron_service
#clnodes: racnode-dc1-1,racnode-dc1-2
tasks:
- name: Manage database
oracle_db:
service_name={{ dbname }}
hostname={{ hostname}}
user=sys
password=Oracle_123
state={{ myaction }}
db_name={{ dbname }}
sid={{ dbsid |default(omit)}}
db_unique_name={{ dbunqname |default(omit) }}
sys_password=Oracle_123
system_password=Oracle_123
responsefile={{ rspfile |default(omit) }}
cdb={{ container |default (omit)}}
initparams={{ initparameters |default(omit)}}
datafile_dest={{ dfd }}
recoveryfile_dest={{rfd}}
storage_type={{storage}}
dbconfig_type={{dbtype}}
racone_service={{ ron_service|default(omit)}}
amm=False
memory_totalmb=1024
nodelist={{ clnodes |default(omit) }}
environment: "{{ oracle_env }}"
run_once: True
'''
import os, re, time
try:
import cx_Oracle
except ImportError:
cx_oracle_exists = False
else:
cx_oracle_exists = True
def get_version(module, msg, oracle_home):
command = '%s/bin/sqlplus -V' % (oracle_home)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
module.fail_json(msg=msg, changed=False)
else:
return stdout.split(' ')[2][0:4]
# Check if the database exists
def check_db_exists(module, msg, oracle_home, db_name, sid, db_unique_name ):
if sid is None:
sid = ''
if gimanaged:
if db_unique_name != None:
checkdb = db_unique_name
else:
checkdb = db_name
command = "%s/bin/srvctl config database -d %s " % (oracle_home, checkdb)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
if 'PRCD-1229' in stdout: #<-- DB is created, but with a different ORACLE_HOME
msg='Database %s already exists in a different home. Stdout -> %s' % (db_name, stdout)
module.fail_json(msg=msg, changed=False)
elif '%s' % (db_name) in stdout: #<-- db doesn't exist
return False
else:
msg = 'Error: command is %s. stdout is %s' % (command, stdout)
return False
elif 'Database name: %s' % (db_name) in stdout: #<-- Database already exist
return True
else:
msg = '%s' % (stdout)
return True
else:
existingdbs = []
oratabfile = '/etc/oratab'
if os.path.exists(oratabfile):
with open(oratabfile) as oratab:
for line in oratab:
if line.startswith('#') or line.startswith(' '):
continue
elif re.search(db_name +':', line) or re.search(sid +':', line):
existingdbs.append(line)
if not existingdbs: #<-- db doesn't exist
return False
else:
for dbs in existingdbs:
if sid != '':
if '%s:' % db_name in dbs or '%s:' % sid in dbs:
if dbs.split(':')[1] != oracle_home.rstrip('/'): #<-- DB is created, but with a different ORACLE_HOME
msg = 'Database %s already exists in a different ORACLE_HOME (%s)' % (db_name, dbs.split(':')[1])
module.fail_json(msg=msg, changed=False)
elif dbs.split(':')[1] == oracle_home.rstrip('/'): #<-- Database already exist
return True
else:
if '%s:' % db_name in dbs:
if dbs.split(':')[1]!= oracle_home.rstrip('/'): #<-- DB is created, but with a different ORACLE_HOME
msg = 'Database %s already exists in a different ORACLE_HOME (%s)' % (db_name, dbs.split(':')[1])
module.fail_json(msg=msg, changed=False)
elif dbs.split(':')[1] == oracle_home.rstrip('/'): #<-- Database already exist
return True
def create_db (module, msg, oracle_home, sys_password, system_password, dbsnmp_password, db_name, sid, db_unique_name, responsefile, template, cdb,
local_undo, datafile_dest, recoveryfile_dest, storage_type, dbconfig_type, racone_service, characterset, memory_percentage, memory_totalmb,
nodelist, db_type, amm, initparams, customscripts, datapatch):
initparam = '-initParams '
paramslist = ''
scriptlist = ''
command = "%s/bin/dbca -createDatabase -silent " % (oracle_home)
if responsefile != None:
if os.path.exists(responsefile):
command += ' -responseFile %s ' % (responsefile)
else:
msg='Responsefile %s doesn\'t exist' % (responsefile)
module.fail_json(msg=msg, changed=False)
if db_unique_name is not None:
initparam += 'db_name=%s,db_unique_name=%s,' % (db_name,db_unique_name)
if initparams is not None:
paramslist = ",".join(initparams)
initparam += '%s' % (paramslist)
else:
command += ' -gdbName %s' % (db_name)
if sid != None:
command += ' -sid %s' % (sid)
if sys_password is not None:
command += ' -sysPassword %s' % (sys_password)
if system_password is not None:
command += ' -systemPassword %s' % (system_password)
else:
system_password = sys_password
command += ' -systemPassword %s' % (system_password)
if dbsnmp_password is not None:
command += ' -dbsnmpPassword %s' % (dbsnmp_password)
else:
dbsnmp_password = sys_password
command += ' -dbsnmpPassword %s' % (dbsnmp_password)
if template:
command += ' -templateName %s' % (template)
if major_version > '11.2':
if cdb == True:
command += ' -createAsContainerDatabase true '
if local_undo == True:
command += ' -useLocalUndoForPDBs true'
else:
command += ' -useLocalUndoForPDBs false'
else:
command += ' -createAsContainerDatabase false '
if datafile_dest != None:
command += ' -datafileDestination %s ' % (datafile_dest)
if recoveryfile_dest != None:
command += ' -recoveryAreaDestination %s ' % (recoveryfile_dest)
if storage_type != None:
command += ' -storageType %s ' % (storage_type)
if dbconfig_type != None:
if dbconfig_type == 'SI':
dbconfig_type = 'SINGLE'
if major_version == '12.2':
command += ' -databaseConfigType %s ' % (dbconfig_type)
elif major_version == '12.1':
command += ' -databaseConfType %s ' % (dbconfig_type)
if dbconfig_type == 'RACONENODE':
if racone_service is None:
racone_service = db_name+'_ronserv'
command += ' -RACOneNodeServiceName %s ' % (racone_service)
if characterset != None:
command += ' -characterSet %s ' % (characterset)
if memory_percentage != None:
command += ' -memoryPercentage %s ' % (memory_percentage)
if memory_totalmb != None:
command += ' -totalMemory %s ' % (memory_totalmb)
if dbconfig_type == 'RAC':
if nodelist != None:
nodelist = ",".join(nodelist)
command += ' -nodelist %s ' % (nodelist)
if db_type != None:
command += ' -databaseType %s ' % (db_type)
if amm != None:
if major_version == '12.2':
if amm == True:
command += ' -memoryMgmtType AUTO '
else:
command += ' -memoryMgmtType AUTO_SGA '
elif major_version == '12.1':
command += ' -automaticMemoryManagement %s ' % (str(amm).lower())
elif major_version == '11.2':
if amm == True:
command += ' -automaticMemoryManagement '
if customscripts is not None:
scriptlist = ",".join(customscripts)
command += ' -customScripts %s ' % (scriptlist)
if db_unique_name != None:
initparam += 'db_name=%s,db_unique_name=%s,' % (db_name,db_unique_name)
if initparams != None:
paramslist = ",".join(initparams)
initparam += ' %s' % (paramslist)
if initparam != '-initParams ' or paramslist != "":
command += initparam
# module.exit_json(msg=command, changed=False)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
module.fail_json(msg=msg, changed=False)
else:
if output == 'short':
return True
else:
verbosemsg = 'STDOUT: %s, COMMAND: %s' % (stdout, command)
verboselist.append(verbosemsg)
return True,verboselist
# module.exit_json(msg=verbosemsg, changed=True)
# elif rc == 0 and datapatch:
# if run_datapatch(module, msg, oracle_home, db_name, db_unique_name, sys_password):
# return True
# else:
# return True
# def run_datapatch(module, msg, oracle_home, db_name, db_unique_name, sys_password):
#
# cursor = getconn(module,msg)
# sid_sql = 'select instance_name from v$instance'
# sid_ = execute_sql_get(module,msg,cursor,sid_sql)
# os.environ['ORACLE_SID'] = sid_[0][0]
#
# if major_version > '11.2':
# command = '%s/OPatch/datapatch -verbose' % (oracle_home)
# (rc, stdout, stderr) = module.run_command(command)
# if rc != 0:
# msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
# module.fail_json(msg=msg, changed=False)
# else:
# return True
# else:
# datapatch_sql = '''
# connect / as sysdba
# @?/rdbms/admin/catbundle.sql psu apply
# exit
# '''
# sqlplus_bin = '%s/bin/sqlplus' % (oracle_home)
# p = subprocess.Popen([sqlplus_bin,'/nolog'],stdin=subprocess.PIPE,
# stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# (stdout,stderr) = p.communicate(datapatch_sql.encode('utf-8'))
# rc = p.returncode
# if rc != 0:
# msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, datapatch_sql)
# module.fail_json(msg=msg, changed=False)
# else:
# return True
def remove_db (module, msg, oracle_home, db_name, sid, db_unique_name, sys_password):
cursor = getconn(module,msg)
israc = ''
israc_sql = 'select parallel,instance_name,host_name from v$instance'
israc_ = execute_sql_get(module,msg,cursor,israc_sql)
remove_db = ''
if gimanaged:
if db_unique_name is not None:
remove_db = db_unique_name
elif sid is not None and israc_[0][0] == 'YES':
remove_db = db_name
elif sid is not None and israc_[0][0] == 'NO':
remove_db = sid
else:
remove_db = db_name
else:
if sid is not None:
remove_db = sid
else:
remove_db = db_name
command = "%s/bin/dbca -deleteDatabase -silent -sourceDB %s -sysDBAUserName sys -sysDBAPassword %s" % (oracle_home, remove_db, sys_password)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg = 'Removal of database %s failed: %s' % (db_name, stdout)
module.fail_json(msg=msg, changed=False)
else:
if output == 'short':
return True
else:
msg = 'STDOUT: %s, COMMAND: %s' % (stdout, command)
module.exit_json(msg=msg, changed=True)
def ensure_db_state (module,msg,oracle_home,db_name,db_unique_name,sid, archivelog, force_logging, flashback,
default_tablespace_type,default_tablespace,default_temp_tablespace):
cursor = getconn(module,msg)
israc = ''
archcomp = ''
flcomp = ''
fbcomp = ''
alterdb_sql = 'alter database'
propsql = "select lower(property_value) from database_properties where property_name in ('DEFAULT_TBS_TYPE','DEFAULT_PERMANENT_TABLESPACE','DEFAULT_TEMP_TABLESPACE') order by 1"
def_tbs_type,def_tbs,def_temp_tbs = execute_sql_get(module,msg,cursor,propsql)
israc_sql = 'select parallel,instance_name,host_name from v$instance'
israc_ = execute_sql_get(module,msg,cursor,israc_sql)
instance_name = israc_[0][1]
host_name = israc_[0][2]
change_restart_sql = []
change_db_sql = []
log_check_sql = 'select log_mode,force_logging, flashback_on from v$database'
log_check_ = execute_sql_get(module,msg,cursor,log_check_sql)
if israc_[0][0] == 'NO':
israc = False
else:
israc = True
if archivelog == True:
archcomp = 'ARCHIVELOG'
archsql = alterdb_sql + ' archivelog'
else:
archcomp = 'NOARCHIVELOG'
archsql = alterdb_sql + ' noarchivelog'
if force_logging == True:
flcomp = 'YES'
flsql = alterdb_sql + ' force logging'
else:
flcomp = 'NO'
flsql = alterdb_sql + ' no force logging'
if flashback == True:
fbcomp = 'YES'
fbsql = alterdb_sql + ' flashback on'
else:
fbcomp = 'NO'
fbsql = alterdb_sql + ' flashback off'
if def_tbs_type[0] != default_tablespace_type:
deftbstypesql = 'alter database set default %s tablespace ' % (default_tablespace_type)
change_db_sql.append(deftbstypesql)
if default_tablespace is not None and def_tbs[0] != default_tablespace:
deftbssql = 'alter database default tablespace %s' % (default_tablespace)
change_db_sql.append(deftbssql)
if default_temp_tablespace is not None and def_temp_tbs[0] != default_temp_tablespace:
deftempsql = 'alter database default temporary tablespace %s' % (default_temp_tablespace)
change_db_sql.append(deftempsql)
if log_check_[0][0] != archcomp:
change_restart_sql.append(archsql)
if log_check_[0][1] != flcomp:
change_db_sql.append(flsql)
if log_check_[0][2] != fbcomp:
change_db_sql.append(fbsql)
if len(change_db_sql) > 0 or len(change_restart_sql) > 0:
if log_check_[0][0] == 'ARCHIVELOG' and log_check_[0][2] == 'YES' and not archivelog and not flashback: # Flashback database needs to be turned off before archivelog is turned off
if len(change_db_sql) > 0: # <- Apply changes that does not require a restart
apply_norestart_changes(module,msg,change_db_sql)
if len(change_restart_sql) > 0: # <- Apply changes that requires a restart
apply_restart_changes(module,msg,oracle_home,db_name,db_unique_name,sid,instance_name,host_name,israc,archcomp,change_restart_sql)
else:
if len(change_restart_sql) > 0: # <- Apply changes that requires a restart
apply_restart_changes(module,msg,oracle_home,db_name,db_unique_name,sid,instance_name,host_name,israc,archcomp,change_restart_sql)
if len(change_db_sql) > 0: # <- Apply changes that does not require a restart
apply_norestart_changes(module,msg,change_db_sql)
msg = 'Database %s has been put in the intended state - Archivelog: %s, Force Logging: %s, Flashback: %s' % (db_name, archivelog,force_logging,flashback)
module.exit_json(msg=msg, changed=True)
else:
if newdb:
msg = 'Database %s successfully created created (%s) ' % (db_name,archcomp)
if output == 'verbose':
msg += ' ,'.join(verboselist)
changed = True
else:
msg = ' Database %s already exists and is in the intended state - Archivelog: %s, Force Logging: %s, Flashback: %s' % (db_name, archivelog,force_logging,flashback)
changed = False
module.exit_json(msg=msg, changed=changed)
def apply_restart_changes(module,msg,oracle_home,db_name,db_unique_name,sid,instance_name,host_name,israc,archcomp,change_restart_sql):
if stop_db(module,msg,oracle_home,db_name,db_unique_name,sid):
if start_instance(module,msg,oracle_home,db_name, db_unique_name,sid,'mount', instance_name, host_name,israc):
time.sleep(10) #<- To allow the DB to register with the listener
cursor = getconn(module,msg)
for sql in change_restart_sql:
execute_sql(module,msg,cursor,sql)
if stop_db(module,msg,oracle_home,db_name,db_unique_name,sid):
if start_db(module,msg,oracle_home,db_name,db_unique_name, sid):
if newdb:
msg = 'Database %s successfully created (%s) ' % (db_name, archcomp)
if output == 'verbose':
msg += ' ,'.join(verboselist)
changed = True
else:
msg = 'Database %s has been put in the intended state - (%s) ' % (db_name,archcomp)
if output == 'verbose':
msg += ' ,'.join(verboselist)
changed = True
def apply_norestart_changes(module,msg,change_db_sql):
cursor = getconn(module,msg)
for sql in change_db_sql:
execute_sql(module,msg,cursor,sql)
def stop_db (module, msg, oracle_home, db_name, db_unique_name, sid):
if gimanaged:
if db_unique_name is not None:
db_name = db_unique_name
command = '%s/bin/srvctl stop database -d %s -o immediate' % (oracle_home,db_name)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
module.fail_json(msg=msg, changed=False)
else:
return True
else:
if sid is not None:
os.environ['ORACLE_SID'] = sid
else:
os.environ['ORACLE_SID'] = db_name
shutdown_sql = '''
connect / as sysdba
shutdown immediate;
exit
'''
sqlplus_bin = '%s/bin/sqlplus' % (oracle_home)
p = subprocess.Popen([sqlplus_bin,'/nolog'],stdin=subprocess.PIPE,
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
(stdout,stderr) = p.communicate(shutdown_sql.encode('utf-8'))
rc = p.returncode
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, shutdown_sql)
module.fail_json(msg=msg, changed=False)
else:
return True
def start_db (module,msg, oracle_home, db_name, db_unique_name, sid):
if gimanaged:
if db_unique_name is not None:
db_name = db_unique_name
command = '%s/bin/srvctl start database -d %s' % (oracle_home,db_name)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
module.fail_json(msg=msg, changed=False)
else:
return True
else:
if sid is not None:
os.environ['ORACLE_SID'] = sid
else:
os.environ['ORACLE_SID'] = db_name
startup_sql = '''
connect / as sysdba
startup;
exit
'''
sqlplus_bin = '%s/bin/sqlplus' % (oracle_home)
p = subprocess.Popen([sqlplus_bin,'/nolog'],stdin=subprocess.PIPE,
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
(stdout,stderr) = p.communicate(startup_sql.encode('utf-8'))
rc = p.returncode
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, startup_sql)
module.fail_json(msg=msg, changed=False)
else:
return True
def start_instance (module,msg, oracle_home, db_name, db_unique_name,sid, open_mode, instance_name, host_name, israc):
if gimanaged:
if db_unique_name is not None:
db_name = db_unique_name
if israc:
command = '%s/bin/srvctl start instance -d %s -i %s' % (oracle_home, db_name, instance_name)
else:
command = '%s/bin/srvctl start database -d %s ' % (oracle_home, db_name)
if open_mode is not None:
command += ' -o %s ' % (open_mode)
(rc, stdout, stderr) = module.run_command(command)
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, command)
module.fail_json(msg=msg, changed=False)
else:
return True
else:
if sid is not None:
os.environ['ORACLE_SID'] = sid
else:
os.environ['ORACLE_SID'] = db_name
startup_sql = '''
connect / as sysdba
startup mount;
exit
'''
sqlplus_bin = '%s/bin/sqlplus' % (oracle_home)
p = subprocess.Popen([sqlplus_bin,'/nolog'],stdin=subprocess.PIPE,
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
(stdout,stderr) = p.communicate(startup_sql.encode('utf-8'))
rc = p.returncode
if rc != 0:
msg = 'Error - STDOUT: %s, STDERR: %s, COMMAND: %s' % (stdout, stderr, shutdown_sql)
module.fail_json(msg=msg, changed=False)
else:
return True
def execute_sql_get(module, msg, cursor, sql):
try:
cursor.execute(sql)
result = (cursor.fetchall())
except cx_Oracle.DatabaseError as exc:
error, = exc.args
msg = 'Something went wrong while executing sql_get - %s sql: %s' % (error.message, sql)
module.fail_json(msg=msg, changed=False)
return False
return result
def execute_sql(module, msg, cursor, sql):
try:
cursor.execute(sql)
except cx_Oracle.DatabaseError as exc:
error, = exc.args
msg = 'Something went wrong while executing sql - %s sql: %s' % (error.message, sql)
module.fail_json(msg=msg, changed=False)
return False
return True
def getconn(module,msg):
hostname = os.uname()[1]
wallet_connect = '/@%s' % service_name
try:
if (not user and not password ): # If neither user or password is supplied, the use of an oracle wallet is assumed
connect = wallet_connect
conn = cx_Oracle.connect(wallet_connect, mode=cx_Oracle.SYSDBA)
elif (user and password ):
dsn = cx_Oracle.makedsn(host=hostname, port=port, service_name=service_name, )
connect = dsn
conn = cx_Oracle.connect(user, password, dsn, mode=cx_Oracle.SYSDBA)
elif (not(user) or not(password)):
module.fail_json(msg='Missing username or password for cx_Oracle')
except cx_Oracle.DatabaseError as exc:
error, = exc.args
msg = 'Could not connect to database - %s, connect descriptor: %s' % (error.message, connect)
module.fail_json(msg=msg, changed=False)
cursor = conn.cursor()
return cursor
def main():
msg = ['']
cursor = None
global gimanaged
global major_version
global user
global password
global service_name
global hostname
global port
global israc
global newdb
global output
global verbosemsg
global verboselist
verbosemsg = ''
verboselist = []
newdb = False
module = AnsibleModule(
argument_spec = dict(
oracle_home = dict(default=None, aliases = ['oh']),
db_name = dict(required=True, aliases = ['db','database_name','name']),
sid = dict(required=False),
db_unique_name = dict(required=False, aliases = ['dbunqn','unique_name']),
sys_password = dict(required=False, no_log=True, aliases = ['syspw','sysdbapassword','sysdbapw']),
system_password = dict(required=False, no_log=True, aliases = ['systempw']),
dbsnmp_password = dict(required=False, no_log=True, aliases = ['dbsnmppw']),
responsefile = dict(required=False),
template = dict(default='General_Purpose.dbc'),
cdb = dict(default=False, type='bool', aliases= ['container']),
local_undo = dict(default=True, type='bool'),
datafile_dest = dict(required=False, aliases= ['dfd']),
recoveryfile_dest = dict(required=False, aliases= ['rfd']),
storage_type = dict(default='FS', aliases= ['storage'],choices = ['FS','ASM']),
dbconfig_type = dict(default='SI',choices = ['SI','RAC','RACONENODE']),
db_type = dict(default='MULTIPURPOSE',choices = ['MULTIPURPOSE','DATA_WAREHOUSING','OLTP']),
racone_service = dict(required=False,aliases = ['ron_service']),
characterset = dict(default='AL32UTF8'),
memory_percentage = dict(required=False),
memory_totalmb = dict(default='1024'),
nodelist = dict(required=False, type='list'),
amm = dict(default=False, type='bool', aliases = ['automatic_memory_management']),
initparams = dict(required=False, type='list'),
customscripts = dict(required=False, type='list'),
default_tablespace_type = dict(default='smallfile',choices = ['smallfile','bigfile']),
default_tablespace = dict(required=False),
default_temp_tablespace = dict(required=False),
archivelog = dict(default=False, type='bool'),
force_logging = dict(default=False, type='bool'),
flashback = dict(default=False, type='bool'),
datapatch = dict(default=True, type='bool'),
output = dict(default="short", choices = ["short","verbose"]),
state = dict(default="present", choices = ["present", "absent", "started"]),
hostname = dict(required=False, default = 'localhost', aliases = ['host']),
port = dict(required=False, default = 1521),
),
mutually_exclusive=[['memory_percentage', 'memory_totalmb']]
)
oracle_home = module.params["oracle_home"]
db_name = module.params["db_name"]
sid = module.params["sid"]
db_unique_name = module.params["db_unique_name"]
sys_password = module.params["sys_password"]
system_password = module.params["system_password"]
dbsnmp_password = module.params["dbsnmp_password"]
responsefile = module.params["responsefile"]
template = module.params["template"]
cdb = module.params["cdb"]
local_undo = module.params["local_undo"]
datafile_dest = module.params["datafile_dest"]
recoveryfile_dest = module.params["recoveryfile_dest"]
storage_type = module.params["storage_type"]
dbconfig_type = module.params["dbconfig_type"]
racone_service = module.params["racone_service"]
characterset = module.params["characterset"]
memory_percentage = module.params["memory_percentage"]
memory_totalmb = module.params["memory_totalmb"]
nodelist = module.params["nodelist"]
db_type = module.params["db_type"]
amm = module.params["amm"]
initparams = module.params["initparams"]
customscripts = module.params["customscripts"]
default_tablespace_type = module.params["default_tablespace_type"]
default_tablespace = module.params["default_tablespace"]
default_temp_tablespace = module.params["default_temp_tablespace"]
archivelog = module.params["archivelog"]
force_logging = module.params["force_logging"]
flashback = module.params["flashback"]
datapatch = module.params["datapatch"]
output = module.params["output"]
state = module.params["state"]
hostname = module.params["hostname"]
port = module.params["port"]
#ld_library_path = '%s/lib' % (oracle_home)
if oracle_home is not None:
os.environ['ORACLE_HOME'] = oracle_home.rstrip('/')
#os.environ['LD_LIBRARY_PATH'] = ld_library_path
elif 'ORACLE_HOME' in os.environ:
oracle_home = os.environ['ORACLE_HOME']
#ld_library_path = os.environ['LD_LIBRARY_PATH']
else:
msg = 'ORACLE_HOME variable not set. Please set it and re-run the command'
module.fail_json(msg=msg, changed=False)
# Decide whether to use srvctl or sqlplus
if os.path.exists('/etc/oracle/olr.loc'):
gimanaged = True
else:
gimanaged = False
if not cx_oracle_exists:
msg = "The cx_Oracle module is required. 'pip install cx_Oracle' should do the trick. If cx_Oracle is installed, make sure ORACLE_HOME & LD_LIBRARY_PATH is set"
module.fail_json(msg=msg)
# Connection details for database
user = 'sys'
password = sys_password
if db_unique_name is not None:
service_name = db_unique_name
else:
service_name = db_name
# Get the Oracle version
major_version = get_version(module,msg,oracle_home)
if state == 'started':
msg = "oracle_home: %s db_name: %s sid: %s db_unique_name: %s" % (oracle_home, db_name, sid, db_unique_name)
if not check_db_exists(module, msg, oracle_home,db_name, sid, db_unique_name):
msg = "Database not found. %s" % msg
module.fail_json(msg=msg, changed=False)
else:
if start_db (module, msg, oracle_home, db_name, db_unique_name, sid):
msg = "Database started."
module.exit_json(msg=msg, changed=True)
else:
msg = "Startup failed. %s" % msg
module.fail_json(msg=msg, changed=False)
elif state == 'present':
if not check_db_exists(module, msg, oracle_home,db_name, sid, db_unique_name):
if create_db(module, msg, oracle_home, sys_password, system_password, dbsnmp_password, db_name, sid, db_unique_name, responsefile, template, cdb, local_undo, datafile_dest, recoveryfile_dest,
storage_type, dbconfig_type, racone_service, characterset, memory_percentage, memory_totalmb, nodelist, db_type, amm, initparams, customscripts,datapatch):
newdb = True
ensure_db_state (module,msg,oracle_home,db_name,db_unique_name,sid, archivelog, force_logging, flashback, default_tablespace_type,default_tablespace,default_temp_tablespace)
else:
module.fail_json(msg=msg, changed=False)
else:
ensure_db_state (module,msg,oracle_home,db_name,db_unique_name,sid, archivelog, force_logging, flashback, default_tablespace_type,default_tablespace,default_temp_tablespace)
# msg = 'Database %s already exists' % (db_name)
# module.exit_json(msg=msg, changed=False)
elif state == 'absent':
if check_db_exists(module, msg, oracle_home, db_name, sid, db_unique_name):
if remove_db(module, msg, oracle_home, db_name, sid, db_unique_name, sys_password):
msg = 'Successfully removed database %s' % (db_name)
module.exit_json(msg=msg, changed=True)
else:
module.fail_json(msg=msg, changed=False)
else:
msg = 'Database %s doesn\'t exist' % (db_name)
module.exit_json(msg=msg, changed=False)