oravirt / ansible-oracle-modules

Oracle modules for Ansible
MIT License
214 stars 159 forks source link

Bequeath Connections - Connecting without service or wallet #89

Open dsjagoda opened 5 years ago

dsjagoda commented 5 years ago

After discovering your modules I came across what appears to be an issue if I want to use a bequeathed connection to the DB (e.g. $ sqlplus / as sysdba). I was using your oracle_role module for testing but a quick check seems to show that the code is the same in modules that connect to a database.

First, here is the relevant information from my db_definition_file that I am reading:

---
host: oradbdvsingle1
fqdn: oradbdvsingle1.lab.com
sid: labdv
oracle_home: /opt/oracle/base/product/12.2.0.1/dbhome_1

Here is the contents of my playbook currently. You'll notice I am not specifying service, username, or password

---
- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    db_definition: "{{ lookup('file', db_definition_file) | from_yaml }}"

  tasks:
    - name: add host(s) to group 'ora_db'...
      add_host:
        name: "{{ db_definition.fqdn }}"
        groups: ora_db
        db: "{{ db_definition }}"

- hosts: ora_db
  become_user: oracle

  tasks:
    - block:
        - name: Add a role
          oracle_role:
            role: testrole
            state: present
            mode: sysdba
          environment:
            ORACLE_HOME: "{{ db.oracle_home }}"
            LD_LIBRARY_PATH: "{{ db.oracle_home }}/lib"
            ORACLE_SID: "{{ db.sid }}"

I modified the oracle_role module in 2 spots:

  1. Change service_name = dict(required=True), to service_name = dict(required=False),
  2. Added/Changed the following in main:
    try:
        if (not user and not password and not service_name): # Assuming that ORACLE_SID environment variable is set and this is a bequeath connection
            if mode == 'sysdba':
                conn = cx_Oracle.connect(mode=cx_Oracle.SYSDBA)
            else:
                conn = cx_Oracle.connect()

        elif (not user and not password ): # If neither user or password is supplied, the use of an oracle wallet is assumed

I'd like to propose this change be propagated to all modules where connections are made to the DB. I can proceed to make the changes but wanted to approach you first in case I'm missing something obvious that you ran into in development.