Alguem me ajudar a converter essa parte do script pra python ??

1. Alguem me ajudar a converter essa parte do script pra python ??

TheMagicOfOz
TheMagicOz

(usa PCLinuxOS)

Enviado em 19/10/2018 - 16:55h

Tenho o seguinte script, o começo dele é em ansible, e o restante é em python, queria converter somente a parte de ansible pra python , SOMENTE A PARTE DO ANSIBLE, mas vou encaminhar o codigo todo:
#!/usr/bin/python
# -*- coding: utf-8 -*-

DOCUMENTATION = '''
---
module: oracle_user
short_description: Manage users/schemas in an Oracle database
description:
- Manage users/schemas in an Oracle database
- Can be run locally on the controlmachine or on a remote host
version_added: "1.9.1"
options:
hostname:
description:
- The Oracle database host
required: false
default: localhost
port:
description:
- The listener port number on the host
required: false
default: 1521
service_name:
description:
- The database service name to connect to
required: true
user:
description:
- The Oracle user name to connect to the database
required: false
password:
description:
- The Oracle user password for 'user'
required: false
mode:
description:
- The mode with which to connect to the database
required: false
default: normal
choices: ['normal','sysdba']
schema:
description:
- The schema that you want to manage
required: false
default: None
schema_password:
description:
- The password for the new schema. i.e '..identified by password'
required: false
default: null
schema_password_hash:
description:
- The password hash for the new schema. i.e '..identified by values 'XXXXXXX'
required: false
default: None
default_tablespace:
description:
- The default tablespace for the new schema. The tablespace must exist
required: false
default: None
default_temp_tablespace:
description:
- The default tablespace for the new schema. The tablespace must exist
required: false
default: None
update_password:
description:
- always will update passwords if they differ. on_create will only set the password for newly created users.
required: false
default: always
choices: ['always','on_create']
authentication_type:
description:
- The type of authentication for the user.
required: false
default: password
choices: ['password','external','global']
profile:
description:
- The profile for the user
required: false
default: None
grants:
description:
- The privileges granted to the new schema
required: false
default: None
state:
description:
- Whether the user should exist. Absent removes the user, locked/unlocked locks or unlocks the user
required: False
default: present
choices: ['present','absent','locked','unlocked']
notes:
- cx_Oracle needs to be installed
requirements: [ "cx_Oracle" ]
author: Mikael Sandström, oravirt@gmail.com, @oravirt
'''

EXAMPLES = '''
# Create a new schema on a remote db by running the module on the controlmachine (i.e: delegate_to: localhost)
oracle_user: hostname=remote-db-server service_name=orcl user=system password=manager schema=myschema schema_password=mypass default_tablespace=test state=present grants="'create session', create any table'"
# Create a new schema on a remote db
oracle_user: hostname=localhost service_name=orcl user=system password=manager schema=myschema schema_password=mypass default_tablespace=test state=present grants=dba
# Drop a schema on a remote db
oracle_user: hostname=localhost service_name=orcl user=system password=manager schema=myschema state=absent
'''

try:
import cx_Oracle
except ImportError:
cx_oracle_exists = False
else:
cx_oracle_exists = True


def clean_string(item):
item = item.replace("'","").replace(", ",",").lstrip(" ").rstrip(",").replace("[","").replace("]","")

return item

def clean_list(item):
item = [p.replace("'","").replace(", ",",").lstrip(" ").rstrip(",").replace("[","").replace("]","") for p in item]

return item

# Check if the user/schema exists
def check_user_exists(msg, cursor, schema):
sql = 'select count(*) from dba_users where username = upper(\'%s\')' % schema

try:
cursor.execute(sql)
result = cursor.fetchone()[0]
except cx_Oracle.DatabaseError, exc:
error, = exc.args
msg = error.message+ 'sql: ' + sql
return False

if result > 0:
msg = 'The schema (%s) already exists' % schema
return True

# Create the user/schema
def create_user(module, msg, cursor, schema, schema_password, schema_password_hash, default_tablespace, default_temp_tablespace, profile, authentication_type, state, container, grants):
grants_list=[]
if not (schema):
msg = 'Error: Missing schema name'
return False

if not (schema_password) and authentication_type == 'password':
if not (schema_password_hash):
msg = 'Error: Missing schema password or password hash'
module.fail_json(msg=msg, Changed=False)


if authentication_type == 'password':
if (schema_password_hash):
sql = 'create user %s identified by values \'%s\' ' % (schema, schema_password_hash)
else:
sql = 'create user %s identified by %s '% (schema, schema_password)
elif authentication_type == 'global':
sql = 'create user %s identified globally ' % (schema)
elif authentication_type == 'external':
sql = 'create user %s identified externally ' % (schema)


if (default_tablespace):
sql += 'default tablespace %s '% default_tablespace
sql += 'quota unlimited on %s '% default_tablespace

if (default_temp_tablespace):
sql += 'temporary tablespace %s '% default_temp_tablespace

if (profile):
sql += ' profile %s' % profile

if (container):
sql += ' container=%s' % (container)

if state == 'locked':
sql += ' account lock'

if state == 'expired':
sql += ' password expire'


try:
cursor.execute(sql)
except cx_Oracle.DatabaseError, exc:
error, = exc.args
msg = 'Blergh, something went wrong while creating the schema - %s sql: %s' % (error.message, sql)
return False

# Add grants to user if explicitly set. If not, only 'create session' is granted
if (grants):
grants=clean_list(grants)
for p in grants:
grants_list.append(p)
grants = ','.join(grants_list)

sql = 'grant %s to %s '% (grants, schema)
if container:
sql += ' container=%s' % (container)
# else:
# sql = 'grant create session to %s '% schema


try:
cursor.execute(sql)
except cx_Oracle.DatabaseError, exc:
error, = exc.args
msg = 'Blergh, something went wrong while adding grants to the schema - %s sql: %s' % (error.message, sql)
return False

return True

# Get the current password hash for the user
def get_user_password_hash(module, msg, cursor, schema):
sql = 'select password from sys.user$ where name = upper(\'%s\')' % schema
try:
cursor.execute(sql)
pwhashresult = cursor.fetchone()[0]
except cx_Oracle.DatabaseError, exc:
error, = exc.args
msg = error.message+ ': sql: ' + sql
module.fail_json(msg=msg)

return pwhashresult

# Modify the user/schema
def modify_user(module, msg, cursor, schema, schema_password, schema_password_hash, default_tablespace, default_temp_tablespace, update_password, profile, authentication_type, state):

sql_get_curr_def = 'select lower(account_status)'
sql = 'alter user %s' % schema

if update_password == 'always':
if authentication_type == 'password':
if schema_password_hash:
sql += ' identified by values \'%s\'' % (schema_password_hash)
elif schema_password:
sql += ' identified by %s ' % (schema_password)
elif authentication_type == 'external':
sql += ' identified externally '
sql_get_curr_def += ' ,lower(authentication_type)'
elif authentication_type == 'global':
sql += ' identified globally '
sql_get_curr_def += ' ,lower(authentication_type)'

if default_tablespace:
sql += ' default tablespace %s' % default_tablespace
sql += ' quota unlimited on %s '% default_tablespace
sql_get_curr_def += ' ,lower(default_tablespace)'

if default_temp_tablespace:
sql += ' temporary tablespace %s ' % default_temp_tablespace
sql_get_curr_def += ' ,lower(temporary_tablespace)'

if profile:
sql += ' profile %s ' % profile
sql_get_curr_def += ' ,lower(profile)'

want_account_status = ''
if state == 'present' or state == 'unlocked':
want_account_status = 'open'
sql += ' account unlock'

elif state == 'locked':
want_account_status = state
sql += ' account lock'

elif state == 'expired':
want_account_status = state
sql += ' password expire'

wanted_list = []
wanted_list.append(want_account_status)

if authentication_type != 'password' and update_password == 'always':
wanted_list.append(authentication_type)

if default_tablespace:
wanted_list.append(default_tablespace)

if default_temp_tablespace:
wanted_list.append(default_temp_tablespace)

if profile:
wanted_list.append(profile)

sql_get_curr_def += ' from dba_users where username = upper(\'%s\')' % schema

if update_password == 'always':
old_pw_hash = get_user_password_hash(module, msg, cursor, schema)

curr_defaults = execute_sql_get(module, msg, cursor, sql_get_curr_def)
curr_defaults = [list(t) for t in curr_defaults]

if (schema_password_hash):
if update_password == 'always':
if (wanted_list in curr_defaults) and (old_pw_hash == schema_password_hash):
# Everything is kosher, exit changed=False
module.exit_json(msg='The schema (%s) is in the intented state' % (schema), changed=False)
else:
# Make the change and exit changed=True
execute_sql(module, msg, cursor, sql)
module.exit_json(msg='Successfully altered the user (%s)' % (schema), changed=True)
else:
if (wanted_list in curr_defaults):
module.exit_json(msg='The schema (%s) is in the intented state' % (schema), changed=False)
else:
# Make the change and exit changed=True
execute_sql(module, msg, cursor, sql)
module.exit_json(msg='Successfully altered the user (%s)' % (schema), changed=True)
else:
if (wanted_list in curr_defaults):
if update_password == 'always':
# change everything and compare hash pre/post. If same => exit change=False else exit change=True
execute_sql(module, msg, cursor, sql)
new_pw_hash = get_user_password_hash(module, msg, cursor, schema)
if new_pw_hash == old_pw_hash:
module.exit_json(msg='The schema (%s) is in the intented state' % (schema), changed=False)
else:
module.exit_json(msg='Successfully altered the user (%s)' % (schema), changed=True)
else:
module.exit_json(msg='The schema (%s) is in the intented state' % (schema), changed=False)
else:
# do the complete change -> exit with change=True
execute_sql(module, msg, cursor, sql)
module.exit_json(msg='Successfully altered the user (%s)' % (schema), changed=True)

return True

# Run the actual modification
def execute_sql(module, msg, cursor, sql):

try:
cursor.execute(sql)
except cx_Oracle.DatabaseError, exc:
error, = exc.args
msg = 'Blergh, something went wrong while executing sql - %s sql: %s' % (error.message, sql)
module.fail_json(msg=msg, changed=False)
return False

return True

def execute_sql_get(module, msg, cursor, sql):

try:
cursor.execute(sql)
result = cursor.fetchall()
except cx_Oracle.DatabaseError, exc:
error, = exc.args
msg = error.message+ ': sql: ' + sql
module.fail_json(msg=msg)

return result


# Drop the user
def drop_user(module, msg, cursor, schema):
black_list = ['sys','system','dbsnmp']
if schema.lower() in black_list:

msg = 'Trying to drop an internal user: %s. Not allowed' % schema
return False

sql = 'drop user %s cascade' % schema

try:
cursor.execute(sql)
except cx_Oracle.DatabaseError, exc:
error, = exc.args
msg = 'Blergh, something went wrong while dropping the schema - %s sql: %s' % (error.message, sql)
return False

return True


def main():

msg = ['']

module = AnsibleModule(
argument_spec = dict(
oracle_home = dict(required=False, aliases=['oh']),
hostname = dict(default='localhost'),
port = dict(default=1521),
service_name = dict(required=True, aliases = ['tns']),
user = dict(required=False),
password = dict(required=False, no_log=True),
mode = dict(default='normal', choices=["normal","sysdba"]),
schema = dict(default=None,aliases=['name']),
schema_password = dict(default=None, no_log=True),
schema_password_hash = dict(default=None, no_log=True),
state = dict(default="present", choices=["present", "absent", "locked", "unlocked", "expired"]),
default_tablespace = dict(default=None),
default_temp_tablespace = dict(default=None),
update_password = dict(default='always', choices=['on_create','always']),
profile = dict(default=None),
authentication_type = dict(default='password', choices=['password','external','global']),
container = dict(default=None),
grants = dict(default=None, type="list")

),
mutually_exclusive=[['schema_password', 'schema_password_hash']]
)

oracle_home = module.params["oracle_home"]
hostname = module.params["hostname"]
port = module.params["port"]
service_name = module.params["service_name"]
user = module.params["user"]
password = module.params["password"]
mode = module.params["mode"]
schema = module.params["schema"]
schema_password = module.params["schema_password"]
schema_password_hash = module.params["schema_password_hash"]
state = module.params["state"]
default_tablespace = module.params["default_tablespace"]
default_temp_tablespace = module.params["default_temp_tablespace"]
update_password = module.params["update_password"]
profile = module.params["profile"]
authentication_type = module.params["authentication_type"]
container = module.params["container"]
grants = module.params["grants"]

if not cx_oracle_exists:
module.fail_json(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")


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']

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
if mode == 'sysdba':
connect = wallet_connect
conn = cx_Oracle.connect(wallet_connect, mode=cx_Oracle.SYSDBA)
else:
connect = wallet_connect
conn = cx_Oracle.connect(wallet_connect)

elif (user and password ):
if mode == 'sysdba':
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)
else:
dsn = cx_Oracle.makedsn(host=hostname, port=port, service_name=service_name)
connect = dsn
conn = cx_Oracle.connect(user, password, dsn)

elif (not(user) or not(password)):
module.fail_json(msg='Missing username or password for cx_Oracle')

except cx_Oracle.DatabaseError, 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()

if state in ('present','expired'):
if not check_user_exists(msg, cursor, schema):
if create_user(module, msg, cursor, schema, schema_password, schema_password_hash, default_tablespace, default_temp_tablespace, profile, authentication_type, state, container, grants):
msg = 'The schema %s has been created successfully' % schema
module.exit_json(msg=msg, changed=True)
else:
module.fail_json(msg=msg, changed=False)
else:
modify_user(module, msg, cursor, schema, schema_password, schema_password_hash, default_tablespace, default_temp_tablespace, update_password, profile, authentication_type, state)

elif state in ('unlocked','locked'):
if not check_user_exists(msg, cursor, schema):
# if create_user(module, msg, cursor, schema, schema_password, schema_password_hash, default_tablespace, default_temp_tablespace, profile, authentication_type, state, container, grants):
msg = 'The schema %s doesn\'t exist' % schema
module.fail_json(msg=msg, changed=False)
else:
modify_user(module, msg, cursor, schema, schema_password, schema_password_hash, default_tablespace, default_temp_tablespace, update_password, profile, authentication_type, state)

elif state == 'absent':
if check_user_exists(msg, cursor, schema):
if drop_user(module, msg, cursor, schema):
msg = 'The schema (%s) has been dropped successfully' % schema
module.exit_json(msg=msg, changed=True)
else:
module.fail_json(msg=msg[0], changed=False)
else:
module.exit_json(msg='The schema (%s) doesn\'t exist' % schema, changed=False)

module.exit_json(msg='Undhandled exit', changed=False)

from ansible.module_utils.basic import *
if __name__ == '__main__':
main()


#############

Ai a parte do ansible é essa que preciso converter para python :

---
module: oracle_user
short_description: Manage users/schemas in an Oracle database
description:
- Manage users/schemas in an Oracle database
- Can be run locally on the controlmachine or on a remote host
version_added: "1.9.1"
options:
hostname:
description:
- The Oracle database host
required: false
default: localhost
port:
description:
- The listener port number on the host
required: false
default: 1521
service_name:
description:
- The database service name to connect to
required: true
user:
description:
- The Oracle user name to connect to the database
required: false
password:
description:
- The Oracle user password for 'user'
required: false
mode:
description:
- The mode with which to connect to the database
required: false
default: normal
choices: ['normal','sysdba']
schema:
description:
- The schema that you want to manage
required: false
default: None
schema_password:
description:
- The password for the new schema. i.e '..identified by password'
required: false
default: null
schema_password_hash:
description:
- The password hash for the new schema. i.e '..identified by values 'XXXXXXX'
required: false
default: None
default_tablespace:
description:
- The default tablespace for the new schema. The tablespace must exist
required: false
default: None
default_temp_tablespace:
description:
- The default tablespace for the new schema. The tablespace must exist
required: false
default: None
update_password:
description:
- always will update passwords if they differ. on_create will only set the password for newly created users.
required: false
default: always
choices: ['always','on_create']
authentication_type:
description:
- The type of authentication for the user.
required: false
default: password
choices: ['password','external','global']
profile:
description:
- The profile for the user
required: false
default: None
grants:
description:
- The privileges granted to the new schema
required: false
default: None
state:
description:
- Whether the user should exist. Absent removes the user, locked/unlocked locks or unlocks the user
required: False
default: present
choices: ['present','absent','locked','unlocked']
notes:
- cx_Oracle needs to be installed
requirements: [ "cx_Oracle" ]
author: Mikael Sandström, oravirt@gmail.com, @oravirt
'''


  


2. Re: Alguem me ajudar a converter essa parte do script pra python ??

Lisandro Guerra
Lisandro

(usa Arch Linux)

Enviado em 19/10/2018 - 18:18h

Olá Mágico. Vammos por partes:
1) O Ansible não é uma linguagem de programação;
2) O trecho que tu queres "converter" está em uma "linguagem de automação" (playbooks);
3) Ferramentas como o Ansible existem justamente par tornar mais fácil a vida, então já vai se preparando que vai ser trabalhoso fazer o que tu pretendes;
4) O trecho em questão confugura e levanta um servidor de DB Oracle local com uma base de dados e um usuário.
Bom, para começar com o trabalho podes pesquisar sobre o módulo "os" que permite executar comandos do sistema operacional e o "cx_Oracle" que é o requerido para o teu programam em Python. lembrando que o Oracle tem que estar instalado ou terás que fazer a obtenção e a instalação também usando Python.

Abraço e sucesso na tarefa.


3. Olá, muito obrigado pela dica!! mas deixa eu te perguntar...

TheMagicOfOz
TheMagicOz

(usa PCLinuxOS)

Enviado em 21/10/2018 - 13:20h

Olá, muito obrigado pela dica!! mas deixa eu te perguntar...
É capaz eu executar um playbook ansible , como esse que tem dentro do script python e desse playbook eu executar o script em python?






Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts