@ -158,7 +158,11 @@ except ImportError:
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . database import SQLParseError , pg_quote_identifier
from ansible . module_utils . database import SQLParseError , pg_quote_identifier
from ansible . module_utils . postgres import connect_to_db , postgres_common_argument_spec
from ansible . module_utils . postgres import (
connect_to_db ,
exec_sql ,
postgres_common_argument_spec ,
)
from ansible . module_utils . _text import to_native
from ansible . module_utils . _text import to_native
@ -238,7 +242,7 @@ class PgOwnership(object):
query . append ( ' TO %s ' % pg_quote_identifier ( self . role , ' role ' ) )
query . append ( ' TO %s ' % pg_quote_identifier ( self . role , ' role ' ) )
query = ' ' . join ( query )
query = ' ' . join ( query )
self . changed = self . __ exec_sql( query , ddl = True )
self . changed = exec_sql( self , query , ddl = True )
def set_owner ( self , obj_type , obj_name ) :
def set_owner ( self , obj_type , obj_name ) :
""" Change owner of a database object.
""" Change owner of a database object.
@ -326,85 +330,59 @@ class PgOwnership(object):
" WHERE matviewname = ' %s ' "
" WHERE matviewname = ' %s ' "
" AND matviewowner = ' %s ' " % ( self . obj_name , self . role ) )
" AND matviewowner = ' %s ' " % ( self . obj_name , self . role ) )
return self . __ exec_sql( query , add_to_executed = False )
return exec_sql( self , query , add_to_executed = False )
def __set_db_owner ( self ) :
def __set_db_owner ( self ) :
""" Set the database owner. """
""" Set the database owner. """
query = " ALTER DATABASE %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' database ' ) ,
query = " ALTER DATABASE %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' database ' ) ,
pg_quote_identifier ( self . role , ' role ' ) )
pg_quote_identifier ( self . role , ' role ' ) )
self . changed = self . __ exec_sql( query , ddl = True )
self . changed = exec_sql( self , query , ddl = True )
def __set_func_owner ( self ) :
def __set_func_owner ( self ) :
""" Set the function owner. """
""" Set the function owner. """
query = " ALTER FUNCTION %s OWNER TO %s " % ( self . obj_name ,
query = " ALTER FUNCTION %s OWNER TO %s " % ( self . obj_name ,
pg_quote_identifier ( self . role , ' role ' ) )
pg_quote_identifier ( self . role , ' role ' ) )
self . changed = self . __ exec_sql( query , ddl = True )
self . changed = exec_sql( self , query , ddl = True )
def __set_seq_owner ( self ) :
def __set_seq_owner ( self ) :
""" Set the sequence owner. """
""" Set the sequence owner. """
query = " ALTER SEQUENCE %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' table ' ) ,
query = " ALTER SEQUENCE %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' table ' ) ,
pg_quote_identifier ( self . role , ' role ' ) )
pg_quote_identifier ( self . role , ' role ' ) )
self . changed = self . __ exec_sql( query , ddl = True )
self . changed = exec_sql( self , query , ddl = True )
def __set_schema_owner ( self ) :
def __set_schema_owner ( self ) :
""" Set the schema owner. """
""" Set the schema owner. """
query = " ALTER SCHEMA %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' schema ' ) ,
query = " ALTER SCHEMA %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' schema ' ) ,
pg_quote_identifier ( self . role , ' role ' ) )
pg_quote_identifier ( self . role , ' role ' ) )
self . changed = self . __ exec_sql( query , ddl = True )
self . changed = exec_sql( self , query , ddl = True )
def __set_table_owner ( self ) :
def __set_table_owner ( self ) :
""" Set the table owner. """
""" Set the table owner. """
query = " ALTER TABLE %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' table ' ) ,
query = " ALTER TABLE %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' table ' ) ,
pg_quote_identifier ( self . role , ' role ' ) )
pg_quote_identifier ( self . role , ' role ' ) )
self . changed = self . __ exec_sql( query , ddl = True )
self . changed = exec_sql( self , query , ddl = True )
def __set_tablespace_owner ( self ) :
def __set_tablespace_owner ( self ) :
""" Set the tablespace owner. """
""" Set the tablespace owner. """
query = " ALTER TABLESPACE %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' database ' ) ,
query = " ALTER TABLESPACE %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' database ' ) ,
pg_quote_identifier ( self . role , ' role ' ) )
pg_quote_identifier ( self . role , ' role ' ) )
self . changed = self . __ exec_sql( query , ddl = True )
self . changed = exec_sql( self , query , ddl = True )
def __set_view_owner ( self ) :
def __set_view_owner ( self ) :
""" Set the view owner. """
""" Set the view owner. """
query = " ALTER VIEW %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' table ' ) ,
query = " ALTER VIEW %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' table ' ) ,
pg_quote_identifier ( self . role , ' role ' ) )
pg_quote_identifier ( self . role , ' role ' ) )
self . changed = self . __ exec_sql( query , ddl = True )
self . changed = exec_sql( self , query , ddl = True )
def __set_mat_view_owner ( self ) :
def __set_mat_view_owner ( self ) :
""" Set the materialized view owner. """
""" Set the materialized view owner. """
query = " ALTER MATERIALIZED VIEW %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' table ' ) ,
query = " ALTER MATERIALIZED VIEW %s OWNER TO %s " % ( pg_quote_identifier ( self . obj_name , ' table ' ) ,
pg_quote_identifier ( self . role , ' role ' ) )
pg_quote_identifier ( self . role , ' role ' ) )
self . changed = self . __ exec_sql( query , ddl = True )
self . changed = exec_sql( self , query , ddl = True )
def __role_exists ( self , role ) :
def __role_exists ( self , role ) :
""" Return True if role exists, otherwise return Fasle. """
""" Return True if role exists, otherwise return Fasle. """
return self . __exec_sql ( " SELECT 1 FROM pg_roles WHERE rolname = ' %s ' " % role , add_to_executed = False )
return exec_sql ( self , " SELECT 1 FROM pg_roles WHERE rolname = ' %s ' " % role , add_to_executed = False )
def __exec_sql ( self , query , ddl = False , add_to_executed = True ) :
""" Execute SQL.
Return a query result if possible or True / False if ddl = True arg was passed .
It ' s necessary for statements that don ' t return any result ( like DDL queries ) .
Arguments :
query ( str ) : SQL query to execute .
ddl ( bool ) : Must return True or False instead of rows
( typical for DDL queries ) ( default False ) .
add_to_executed ( bool ) : Append the query to self . executed_queries attr .
"""
try :
self . cursor . execute ( query )
if add_to_executed :
self . executed_queries . append ( query )
if not ddl :
res = self . cursor . fetchall ( )
return res
return True
except Exception as e :
self . module . fail_json ( msg = " Cannot execute SQL ' %s ' : %s " % ( query , to_native ( e ) ) )
return False
# ===========================================
# ===========================================