Skip to content

Commit 999f7cc

Browse files
Merge pull request #37 from thedatabaseme/develop
Merge oracle_sql module usage to master
2 parents 444a132 + 91f6b49 commit 999f7cc

File tree

10 files changed

+192
-49
lines changed

10 files changed

+192
-49
lines changed

.github/workflows/ansible_lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

1818
- name: Lint Ansible Playbook
1919
# replace "master" with any valid ref
20-
uses: ansible/ansible-lint-action@master
20+
uses: ansible/ansible-lint-action@main
2121
with:
2222
# [required]
2323
# Paths to ansible files (i.e., playbooks, tasks, handlers etc..)

Changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Changelog of Pythia:
22

3+
Version 2.3.5
4+
- New Feature #34: using the shell command with sqlplus has been replaced by the oracle_sql
5+
module
6+
37
Version 2.3.0
48
- New Feature #25: You can now specify a list of instance parameters which will get set
59
when a database is getting created. See docs/CREATE_DB.md

docs/07_DATAPUMP.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Datapump Export / Import a Database with Pythia
22

3-
# UPDATE STARTS HERE!
4-
53
Pythia is able to do a Datapump Export / Import over a NETWORK_LINK (Database Link). You may either do a FULL Export / Import or an Import of a List of Schemas. Pythia also supports you by remapping the schemas.
64

75
Be aware, that all restrictions for doing an Export / Import over a Database Link are applying here. More informations can be found [here](https://docs.oracle.com/database/121/SUTIL/GUID-0871E56B-07EB-43B3-91DA-D1F457CF6182.htm#SUTIL919)

roles/pythia/library/oracle_sql.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/python
2+
3+
# Copyright: (c) 2022, Philip Haberkern / TheDatabaseMe <[email protected]>
4+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
from __future__ import (absolute_import, division, print_function)
6+
__metaclass__ = type
7+
8+
DOCUMENTATION = r'''
9+
---
10+
module: oracle_sql
11+
12+
short_description: This module runs SQL queries on an Oracle database
13+
14+
version_added: "1.0.0"
15+
16+
description: This module runs SQL queries on an Oracle database.
17+
18+
options:
19+
orahome:
20+
description: Oracle Home Directory.
21+
type: str
22+
required: true
23+
sql:
24+
description: SQL command to run on Oracle database.
25+
type: str
26+
required: true
27+
28+
author:
29+
- Philip Haberkern (@thedatabaseme)
30+
'''
31+
32+
EXAMPLES = r'''
33+
- name: Run SQL query on Oracle database
34+
oracle_sql:
35+
orahome: "/oracle/product/19_ENT/"
36+
sql: "select * from dba_users where username = 'SYSTEM';"
37+
environment:
38+
ORACLE_HOME: "/oracle/product/19_ENT"
39+
ORACLE_SID: "ORA19"
40+
become: yes
41+
become_user: "oracle"
42+
'''
43+
44+
RETURN = r'''
45+
query_command:
46+
description: The final sqlplus command after it has been built.
47+
type: str
48+
returned: always
49+
sample: "echo select account_status from dba_users where username = 'SYSTEM' | /oracle/product/19_ENT/bin/sqlplus -s / as sysdba"
50+
query_result:
51+
description: The query result / output that the module generates.
52+
type: str
53+
returned: always
54+
sample: 'OPEN'
55+
'''
56+
57+
from ansible.module_utils.basic import AnsibleModule
58+
59+
60+
def run_module():
61+
# define available arguments/parameters a user can pass to the module
62+
module_args = dict(
63+
orahome=dict(type='str', required=True),
64+
sql=dict(type='str', required=True)
65+
)
66+
67+
module = AnsibleModule(
68+
argument_spec=module_args,
69+
supports_check_mode=True
70+
)
71+
72+
if module.check_mode:
73+
module.exit_json(msg="Running |" + module.params['sql'] )
74+
75+
cmd='echo "' + module.params['sql'] + '" | ' + module.params['orahome'] + '/bin/sqlplus -s / as sysdba'
76+
rc, out, err = module.run_command(cmd, use_unsafe_shell=True)
77+
78+
if rc != 0:
79+
module.fail_json(msg='failed with error:' + err)
80+
81+
result = dict(
82+
changed=True,
83+
query_command=cmd,
84+
query_result=out,
85+
rc=rc
86+
)
87+
88+
if module.params['sql'].lower().startswith('select'):
89+
result['changed']=False
90+
91+
# in the event of a successful module execution, you will want to
92+
# simple AnsibleModule.exit_json(), passing the key/value results
93+
module.exit_json(**result)
94+
95+
96+
def main():
97+
run_module()
98+
99+
100+
if __name__ == '__main__':
101+
main()

roles/pythia/tasks/datapump.yml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
- name: Check if Target Database is Open
2121
become: true
2222
become_user: "{{ oracle_user }}"
23-
shell: "echo -e \"select status from v\\$instance;\" | {{ datapump_target_home.stdout }}/bin/sqlplus -s / as sysdba"
24-
changed_when: false
23+
oracle_sql:
24+
orahome: "{{ datapump_target_home.stdout }}"
25+
sql: "select status from v\\$instance;"
2526
environment:
2627
ORACLE_HOME: "{{ datapump_target_home.stdout }}"
2728
ORACLE_SID: "{{ datapump_target_sid }}"
@@ -33,7 +34,7 @@
3334

3435
- name: Oracle SID for Import is NOT in OPEN State
3536
assert:
36-
that: '"OPEN" | string in item.stdout'
37+
that: '"OPEN" | string in item.query_result'
3738
fail_msg: The Target Database {{ datapump_target_sid }} is not in OPEN State.
3839
success_msg: Oracle SID {{ datapump_target_sid }} is in OPEN State. Proceed with Datapump.
3940
become: true
@@ -68,10 +69,11 @@
6869
- name: Drop maybe existing Database Link to Source Database {{ datapump_source_sid }}
6970
become: true
7071
become_user: "{{ oracle_user }}"
71-
shell: "echo -e \"set heading off; \\n drop public database link pythia_dp_{{ datapump_source_sid }};\" | {{ datapump_target_home.stdout }}/bin/sqlplus -s / as sysdba" # noqa line-length
72-
changed_when: true
72+
oracle_sql:
73+
orahome: "{{ datapump_target_home.stdout }}"
74+
sql: "drop public database link pythia_dp_{{ datapump_source_sid }};"
7375
register: result
74-
failed_when: ("FATAL" in result.stdout)
76+
failed_when: ("FATAL" in result.query_result)
7577
environment:
7678
ORACLE_HOME: "{{ datapump_target_home.stdout }}"
7779
ORACLE_SID: "{{ datapump_target_sid }}"
@@ -83,8 +85,9 @@
8385
- name: Create Database Link to Source Database {{ datapump_source_sid }}
8486
become: true
8587
become_user: "{{ oracle_user }}"
86-
shell: "echo -e \"set heading off; \\n create public database link pythia_dp_{{ datapump_source_sid }} connect to {{ datapump_source_user }} identified by \"{{ lookup('vars', 'vault_datapump_' + datapump_source_sid) }}\" using 'PYTHIA_DP_{{ datapump_source_sid }}';\" | {{ datapump_target_home.stdout }}/bin/sqlplus -s / as sysdba" # noqa line-length
87-
changed_when: true
88+
oracle_sql:
89+
orahome: "{{ datapump_target_home.stdout }}"
90+
sql: "create public database link pythia_dp_{{ datapump_source_sid }} connect to {{ datapump_source_user }} identified by {{ lookup('vars', 'vault_datapump_' + datapump_source_sid) }} using 'PYTHIA_DP_{{ datapump_source_sid }}';" # noqa line-length
8891
environment:
8992
ORACLE_HOME: "{{ datapump_target_home.stdout }}"
9093
ORACLE_SID: "{{ datapump_target_sid }}"
@@ -96,8 +99,9 @@
9699
- name: Create Directory Object for Logfiles
97100
become: true
98101
become_user: "{{ oracle_user }}"
99-
shell: "echo -e \"set heading off; \\n create or replace directory PYTHIA_DP_DIR as '{{ remote_stage_directory }}';\" | {{ datapump_target_home.stdout }}/bin/sqlplus -s / as sysdba" # noqa line-length
100-
changed_when: true
102+
oracle_sql:
103+
orahome: "{{ datapump_target_home.stdout }}"
104+
sql: "create or replace directory PYTHIA_DP_DIR as '{{ remote_stage_directory }}';"
101105
environment:
102106
ORACLE_HOME: "{{ datapump_target_home.stdout }}"
103107
ORACLE_SID: "{{ datapump_target_sid }}"
@@ -136,7 +140,7 @@
136140
async: "{{ datapump_max_runtime }}"
137141
poll: 30
138142
register: result
139-
failed_when: ("FATAL" in result.stdout)
143+
failed_when: ("fatal" in result.stderr_lines)
140144
no_log: true
141145
tags:
142146
- datapump

roles/pythia/tasks/duplicate.yml

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@
6565
- name: Check if Source Database is Open
6666
become: true
6767
become_user: "{{ oracle_user }}"
68-
shell: "echo -e \"select status from v\\$instance;\" | {{ duplicate_source_home.stdout }}/bin/sqlplus -s / as sysdba"
69-
changed_when: false
68+
oracle_sql:
69+
orahome: "{{ duplicate_source_home.stdout }}"
70+
sql: "select status from v\\$instance;"
7071
environment:
7172
ORACLE_HOME: "{{ duplicate_source_home.stdout }}"
7273
ORACLE_SID: "{{ duplicate_source_sid }}"
@@ -78,7 +79,7 @@
7879

7980
- name: Oracle SID to Duplicate is NOT in OPEN State
8081
assert:
81-
that: '"OPEN" | string in item.stdout'
82+
that: '"OPEN" | string in item.query_result'
8283
fail_msg: The Source Database {{ duplicate_source_sid }} is not in OPEN State.
8384
success_msg: Oracle SID {{ duplicate_source_sid }} is in OPEN State. Proceed with Duplicate.
8485
become: true
@@ -200,8 +201,9 @@
200201
- name: Check if Target Database is Open
201202
become: true
202203
become_user: "{{ oracle_user }}"
203-
shell: "echo -e \"set heading off; \\n set feedback off; \\n select status from v\\$instance;\" | {{ duplicate_source_home.stdout }}/bin/sqlplus -s / as sysdba" # noqa line-length
204-
changed_when: false
204+
oracle_sql:
205+
orahome: "{{ duplicate_source_home.stdout }}"
206+
sql: "select status from v\\$instance;"
205207
environment:
206208
ORACLE_HOME: "{{ duplicate_source_home.stdout }}"
207209
ORACLE_SID: "{{ duplicate_target_sid }}"
@@ -213,7 +215,7 @@
213215

214216
- name: Oracle SID on Target System is in OPEN State
215217
assert:
216-
that: '"OPEN" | string not in item.stdout'
218+
that: '"OPEN" | string not in item.query_result'
217219
fail_msg: "The Source Database {{ duplicate_target_sid }} is in OPEN State. If you want to remove it prior to duplication, specify so in the duplicate_remove_target variable!" # noqa line-length
218220
success_msg: "Oracle SID {{ duplicate_target_sid }} is not in OPEN State. Proceed with Duplicate."
219221
become: true
@@ -234,20 +236,21 @@
234236
environment:
235237
ORACLE_HOME: "{{ duplicate_source_home.stdout }}"
236238
ORACLE_SID: "{{ duplicate_target_sid }}"
237-
register: target_controlfiles
239+
register: target_passwords
238240
with_items:
239241
- "{{ target_db_status }}"
240242
delegate_to: "{{ duplicate_target_host }}"
241243
run_once: true
242-
when: (duplicate_preserve_passwords) and ("OPEN" | string in item.stdout_lines)
244+
when: (duplicate_preserve_passwords) and ("OPEN" | string in item.query_result)
243245
tags:
244246
- duplicate
245247

246248
- name: Gather Controlfiles from running Target Database
247249
become: true
248250
become_user: "{{ oracle_user }}"
249-
shell: "echo -e \"set heading off; \\n set lines 500; \\n set feedback off; \\n select value from v\\$parameter where name = 'control_files';\" | {{ duplicate_source_home.stdout }}/bin/sqlplus -s / as sysdba" # noqa line-length
250-
changed_when: false
251+
oracle_sql:
252+
orahome: "{{ duplicate_source_home.stdout }}"
253+
sql: "select value from v\\$parameter where name = 'control_files';"
251254
environment:
252255
ORACLE_HOME: "{{ duplicate_source_home.stdout }}"
253256
ORACLE_SID: "{{ duplicate_target_sid }}"
@@ -256,14 +259,16 @@
256259
- "{{ target_db_status }}"
257260
delegate_to: "{{ duplicate_target_host }}"
258261
run_once: true
259-
when: (duplicate_remove_target) and ("OPEN" | string in item.stdout_lines)
262+
when: (duplicate_remove_target) and ("OPEN" | string in item.query_result)
260263
tags:
261264
- duplicate
262265

263266
- name: Gather Datafiles from running Target Database
264267
become: true
265268
become_user: "{{ oracle_user }}"
266-
shell: "echo -e \"set heading off; \\n set lines 500; \\n set pages 500; \\n set feedback off; \\n select file_name from dba_data_files;\" | {{ duplicate_source_home.stdout }}/bin/sqlplus -s / as sysdba" # noqa line-length
269+
oracle_sql:
270+
orahome: "{{ duplicate_source_home.stdout }}"
271+
sql: "select file_name from dba_data_files;"
267272
environment:
268273
ORACLE_HOME: "{{ duplicate_source_home.stdout }}"
269274
ORACLE_SID: "{{ duplicate_target_sid }}"
@@ -272,14 +277,16 @@
272277
- "{{ target_db_status }}"
273278
delegate_to: "{{ duplicate_target_host }}"
274279
run_once: true
275-
when: (duplicate_remove_target) and ("OPEN" | string in item.stdout)
280+
when: (duplicate_remove_target) and ("OPEN" | string in item.query_result)
276281
tags:
277282
- duplicate
278283

279284
- name: Gather Tempfiles from running Target Database
280285
become: true
281286
become_user: "{{ oracle_user }}"
282-
shell: "echo -e \"set heading off; \\n set lines 500; \\n set pages 500; \\n set feedback off; \\n select file_name from dba_temp_files;\" | {{ duplicate_source_home.stdout }}/bin/sqlplus -s / as sysdba" # noqa line-length
287+
oracle_sql:
288+
orahome: "{{ duplicate_source_home.stdout }}"
289+
sql: "select file_name from dba_temp_files;"
283290
environment:
284291
ORACLE_HOME: "{{ duplicate_source_home.stdout }}"
285292
ORACLE_SID: "{{ duplicate_target_sid }}"
@@ -288,14 +295,16 @@
288295
- "{{ target_db_status }}"
289296
delegate_to: "{{ duplicate_target_host }}"
290297
run_once: true
291-
when: (duplicate_remove_target) and ("OPEN" | string in item.stdout)
298+
when: (duplicate_remove_target) and ("OPEN" | string in item.query_result)
292299
tags:
293300
- duplicate
294301

295302
- name: Gather Redologs from running Target Database
296303
become: true
297304
become_user: "{{ oracle_user }}"
298-
shell: "echo -e \"set heading off; \\n set lines 500; \\n set pages 500; \\n set feedback off; \\n select member from v\\$logfile;\" | {{ duplicate_source_home.stdout }}/bin/sqlplus -s / as sysdba" # noqa line-length
305+
oracle_sql:
306+
orahome: "{{ duplicate_source_home.stdout }}"
307+
sql: "select member from v\\$logfile;"
299308
environment:
300309
ORACLE_HOME: "{{ duplicate_source_home.stdout }}"
301310
ORACLE_SID: "{{ duplicate_target_sid }}"
@@ -304,7 +313,7 @@
304313
- "{{ target_db_status }}"
305314
delegate_to: "{{ duplicate_target_host }}"
306315
run_once: true
307-
when: (duplicate_remove_target) and ("OPEN" | string in item.stdout)
316+
when: (duplicate_remove_target) and ("OPEN" | string in item.query_result)
308317
tags:
309318
- duplicate
310319

@@ -319,7 +328,7 @@
319328
- "{{ target_db_status }}"
320329
delegate_to: "{{ duplicate_target_host }}"
321330
run_once: true
322-
when: (duplicate_remove_target) and ("OPEN" | string in item.stdout)
331+
when: (duplicate_remove_target) and ("OPEN" | string in item.query_result)
323332
tags:
324333
- duplicate
325334

@@ -675,7 +684,9 @@
675684
- name: Shutdown Target Database for restoring the original SPFILE
676685
become: true
677686
become_user: "{{ oracle_user }}"
678-
shell: "echo -e \"shutdown immediate;\" | {{ duplicate_source_home.stdout }}/bin/sqlplus -s / as sysdba"
687+
oracle_sql:
688+
orahome: "{{ duplicate_source_home.stdout }}"
689+
sql: "shutdown immediate;"
679690
environment:
680691
ORACLE_HOME: "{{ duplicate_source_home.stdout }}"
681692
ORACLE_SID: "{{ duplicate_target_sid }}"
@@ -702,7 +713,9 @@
702713
- name: Startup Target Database for restoring the original SPFILE
703714
become: true
704715
become_user: "{{ oracle_user }}"
705-
shell: "echo -e \"startup;\" | {{ duplicate_source_home.stdout }}/bin/sqlplus -s / as sysdba"
716+
oracle_sql:
717+
orahome: "{{ duplicate_source_home.stdout }}"
718+
sql: "startup;"
706719
environment:
707720
ORACLE_HOME: "{{ duplicate_source_home.stdout }}"
708721
ORACLE_SID: "{{ duplicate_target_sid }}"

roles/pythia/tasks/install_db.yml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,16 @@
114114
- name: Setting SYS and SYSTEM Password after Creation
115115
become: true
116116
become_user: "{{ oracle_user }}"
117-
shell: "{{ item }}"
118-
changed_when: true
117+
# shell: "{{ item }}"
118+
# changed_when: true
119+
oracle_sql:
120+
orahome: "{{ rdbms[oracle_version|string].oracle_home }}"
121+
sql: "{{ item }}"
119122
with_items:
120-
- "echo -e \"alter user system identified by \"{{ system_user_password }}\";\" | {{ rdbms[oracle_version|string].oracle_home }}/bin/sqlplus -s / as sysdba"
121-
- "echo -e \"alter user system identified by \"{{ sys_user_password }}\";\" | {{ rdbms[oracle_version|string].oracle_home }}/bin/sqlplus -s / as sysdba"
123+
# - "echo -e \"alter user system identified by \"{{ system_user_password }}\";\" | {{ rdbms[oracle_version|string].oracle_home }}/bin/sqlplus -s / as sysdba"
124+
# - "echo -e \"alter user system identified by \"{{ sys_user_password }}\";\" | {{ rdbms[oracle_version|string].oracle_home }}/bin/sqlplus -s / as sysdba"
125+
- 'alter user system identified by "{{ system_user_password }}";'
126+
- 'alter user sys identified by "{{ sys_user_password }}";'
122127
environment:
123128
ORACLE_HOME: "{{ rdbms[oracle_version|string].oracle_home }}"
124129
ORACLE_SID: "{{ oracle_sid }}"
@@ -148,7 +153,10 @@
148153
- name: Setting Password for Default DB Users
149154
become: true
150155
become_user: "{{ oracle_user }}"
151-
shell: "echo -e \"alter user {{ item }} identified by \"{{ default_user_password }}\";\" | {{ rdbms[oracle_version|string].oracle_home }}/bin/sqlplus -s / as sysdba" # noqa line-length
156+
# shell: "echo -e \"alter user {{ item }} identified by \"{{ default_user_password }}\";\" | {{ rdbms[oracle_version|string].oracle_home }}/bin/sqlplus -s / as sysdba" # noqa line-length
157+
oracle_sql:
158+
orahome: "{{ rdbms[oracle_version|string].oracle_home }}"
159+
sql: 'alter user {{ item }} identified by "{{ default_user_password }}";'
152160
environment:
153161
ORACLE_HOME: "{{ rdbms[oracle_version|string].oracle_home }}"
154162
ORACLE_SID: "{{ oracle_sid }}"

0 commit comments

Comments
 (0)