| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: vertica_info |
| 11 |
short_description: Gathers Vertica database facts |
| 12 |
description: |
| 13 |
- Gathers Vertica database information. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
- community.general._attributes.info_module |
| 17 |
options: |
| 18 |
cluster: |
| 19 |
description: |
| 20 |
- Name of the cluster running the schema. |
| 21 |
default: localhost |
| 22 |
type: str |
| 23 |
port: |
| 24 |
description: Database port to connect to. |
| 25 |
default: '5433' |
| 26 |
type: str |
| 27 |
db: |
| 28 |
description: |
| 29 |
- Name of the database running the schema. |
| 30 |
type: str |
| 31 |
login_user: |
| 32 |
description: |
| 33 |
- The username used to authenticate with. |
| 34 |
default: dbadmin |
| 35 |
type: str |
| 36 |
login_password: |
| 37 |
description: |
| 38 |
- The password used to authenticate with. |
| 39 |
type: str |
| 40 |
notes: |
| 41 |
- The default authentication assumes that you are either logging in as or sudo'ing to the C(dbadmin) account on the host. |
| 42 |
- This module uses C(pyodbc), a Python ODBC database adapter. You must ensure that C(unixODBC) and C(pyodbc) are installed |
| 43 |
on the host and properly configured. |
| 44 |
- Configuring C(unixODBC) for Vertica requires C(Driver = /opt/vertica/lib64/libverticaodbc.so) to be added to the C(Vertica) |
| 45 |
section of either C(/etc/odbcinst.ini) or C($HOME/.odbcinst.ini) and both C(ErrorMessagesPath = /opt/vertica/lib64) and |
| 46 |
C(DriverManagerEncoding = UTF-16) to be added to the C(Driver) section of either C(/etc/vertica.ini) or C($HOME/.vertica.ini). |
| 47 |
requirements: ['unixODBC', 'pyodbc'] |
| 48 |
author: "Dariusz Owczarek (@dareko)" |
| 49 |
""" |
| 50 |
|
| 51 |
EXAMPLES = r""" |
| 52 |
- name: Gathering vertica facts |
| 53 |
community.general.vertica_info: db=db_name |
| 54 |
register: result |
| 55 |
|
| 56 |
- name: Print schemas |
| 57 |
ansible.builtin.debug: |
| 58 |
msg: "{{ result.vertica_schemas }}" |
| 59 |
""" |
| 60 |
import traceback |
| 61 |
|
| 62 |
PYODBC_IMP_ERR = None |
| 63 |
try: |
| 64 |
import pyodbc |
| 65 |
except ImportError: |
| 66 |
PYODBC_IMP_ERR = traceback.format_exc() |
| 67 |
pyodbc_found = False |
| 68 |
else: |
| 69 |
pyodbc_found = True |
| 70 |
|
| 71 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 72 |
|
| 73 |
|
| 74 |
class NotSupportedError(Exception): |
| 75 |
pass |
| 76 |
|
| 77 |
|
| 78 |
# module specific functions |
| 79 |
|
| 80 |
|
| 81 |
def get_schema_facts(cursor, schema=""): |
| 82 |
facts = {} |
| 83 |
cursor.execute( |
| 84 |
""" |
| 85 |
select schema_name, schema_owner, create_time |
| 86 |
from schemata |
| 87 |
where not is_system_schema and schema_name not in ('public') |
| 88 |
and (? = '' or schema_name ilike ?) |
| 89 |
""", |
| 90 |
schema, |
| 91 |
schema, |
| 92 |
) |
| 93 |
while True: |
| 94 |
rows = cursor.fetchmany(100) |
| 95 |
if not rows: |
| 96 |
break |
| 97 |
for row in rows: |
| 98 |
facts[row.schema_name.lower()] = { |
| 99 |
"name": row.schema_name, |
| 100 |
"owner": row.schema_owner, |
| 101 |
"create_time": str(row.create_time), |
| 102 |
"usage_roles": [], |
| 103 |
"create_roles": [], |
| 104 |
} |
| 105 |
cursor.execute( |
| 106 |
""" |
| 107 |
select g.object_name as schema_name, r.name as role_name, |
| 108 |
lower(g.privileges_description) privileges_description |
| 109 |
from roles r join grants g |
| 110 |
on g.grantee = r.name and g.object_type='SCHEMA' |
| 111 |
and g.privileges_description like '%USAGE%' |
| 112 |
and g.grantee not in ('public', 'dbadmin') |
| 113 |
and (? = '' or g.object_name ilike ?) |
| 114 |
""", |
| 115 |
schema, |
| 116 |
schema, |
| 117 |
) |
| 118 |
while True: |
| 119 |
rows = cursor.fetchmany(100) |
| 120 |
if not rows: |
| 121 |
break |
| 122 |
for row in rows: |
| 123 |
schema_key = row.schema_name.lower() |
| 124 |
if "create" in row.privileges_description: |
| 125 |
facts[schema_key]["create_roles"].append(row.role_name) |
| 126 |
else: |
| 127 |
facts[schema_key]["usage_roles"].append(row.role_name) |
| 128 |
return facts |
| 129 |
|
| 130 |
|
| 131 |
def get_user_facts(cursor, user=""): |
| 132 |
facts = {} |
| 133 |
cursor.execute( |
| 134 |
""" |
| 135 |
select u.user_name, u.is_locked, u.lock_time, |
| 136 |
p.password, p.acctexpired as is_expired, |
| 137 |
u.profile_name, u.resource_pool, |
| 138 |
u.all_roles, u.default_roles |
| 139 |
from users u join password_auditor p on p.user_id = u.user_id |
| 140 |
where not u.is_super_user |
| 141 |
and (? = '' or u.user_name ilike ?) |
| 142 |
""", |
| 143 |
user, |
| 144 |
user, |
| 145 |
) |
| 146 |
while True: |
| 147 |
rows = cursor.fetchmany(100) |
| 148 |
if not rows: |
| 149 |
break |
| 150 |
for row in rows: |
| 151 |
user_key = row.user_name.lower() |
| 152 |
facts[user_key] = { |
| 153 |
"name": row.user_name, |
| 154 |
"locked": str(row.is_locked), |
| 155 |
"password": row.password, |
| 156 |
"expired": str(row.is_expired), |
| 157 |
"profile": row.profile_name, |
| 158 |
"resource_pool": row.resource_pool, |
| 159 |
"roles": [], |
| 160 |
"default_roles": [], |
| 161 |
} |
| 162 |
if row.is_locked: |
| 163 |
facts[user_key]["locked_time"] = str(row.lock_time) |
| 164 |
if row.all_roles: |
| 165 |
facts[user_key]["roles"] = row.all_roles.replace(" ", "").split(",") |
| 166 |
if row.default_roles: |
| 167 |
facts[user_key]["default_roles"] = row.default_roles.replace(" ", "").split(",") |
| 168 |
return facts |
| 169 |
|
| 170 |
|
| 171 |
def get_role_facts(cursor, role=""): |
| 172 |
facts = {} |
| 173 |
cursor.execute( |
| 174 |
""" |
| 175 |
select r.name, r.assigned_roles |
| 176 |
from roles r |
| 177 |
where (? = '' or r.name ilike ?) |
| 178 |
""", |
| 179 |
role, |
| 180 |
role, |
| 181 |
) |
| 182 |
while True: |
| 183 |
rows = cursor.fetchmany(100) |
| 184 |
if not rows: |
| 185 |
break |
| 186 |
for row in rows: |
| 187 |
role_key = row.name.lower() |
| 188 |
facts[role_key] = {"name": row.name, "assigned_roles": []} |
| 189 |
if row.assigned_roles: |
| 190 |
facts[role_key]["assigned_roles"] = row.assigned_roles.replace(" ", "").split(",") |
| 191 |
return facts |
| 192 |
|
| 193 |
|
| 194 |
def get_configuration_facts(cursor, parameter=""): |
| 195 |
facts = {} |
| 196 |
cursor.execute( |
| 197 |
""" |
| 198 |
select c.parameter_name, c.current_value, c.default_value |
| 199 |
from configuration_parameters c |
| 200 |
where c.node_name = 'ALL' |
| 201 |
and (? = '' or c.parameter_name ilike ?) |
| 202 |
""", |
| 203 |
parameter, |
| 204 |
parameter, |
| 205 |
) |
| 206 |
while True: |
| 207 |
rows = cursor.fetchmany(100) |
| 208 |
if not rows: |
| 209 |
break |
| 210 |
for row in rows: |
| 211 |
facts[row.parameter_name.lower()] = { |
| 212 |
"parameter_name": row.parameter_name, |
| 213 |
"current_value": row.current_value, |
| 214 |
"default_value": row.default_value, |
| 215 |
} |
| 216 |
return facts |
| 217 |
|
| 218 |
|
| 219 |
def get_node_facts(cursor, schema=""): |
| 220 |
facts = {} |
| 221 |
cursor.execute(""" |
| 222 |
select node_name, node_address, export_address, node_state, node_type, |
| 223 |
catalog_path |
| 224 |
from nodes |
| 225 |
""") |
| 226 |
while True: |
| 227 |
rows = cursor.fetchmany(100) |
| 228 |
if not rows: |
| 229 |
break |
| 230 |
for row in rows: |
| 231 |
facts[row.node_address] = { |
| 232 |
"node_name": row.node_name, |
| 233 |
"export_address": row.export_address, |
| 234 |
"node_state": row.node_state, |
| 235 |
"node_type": row.node_type, |
| 236 |
"catalog_path": row.catalog_path, |
| 237 |
} |
| 238 |
return facts |
| 239 |
|
| 240 |
|
| 241 |
# module logic |
| 242 |
|
| 243 |
|
| 244 |
def main(): |
| 245 |
module = AnsibleModule( |
| 246 |
argument_spec=dict( |
| 247 |
cluster=dict(default="localhost"), |
| 248 |
port=dict(default="5433"), |
| 249 |
db=dict(), |
| 250 |
login_user=dict(default="dbadmin"), |
| 251 |
login_password=dict(no_log=True), |
| 252 |
), |
| 253 |
supports_check_mode=True, |
| 254 |
) |
| 255 |
|
| 256 |
if not pyodbc_found: |
| 257 |
module.fail_json(msg=missing_required_lib("pyodbc"), exception=PYODBC_IMP_ERR) |
| 258 |
|
| 259 |
db = "" |
| 260 |
if module.params["db"]: |
| 261 |
db = module.params["db"] |
| 262 |
|
| 263 |
try: |
| 264 |
dsn = ( |
| 265 |
"Driver=Vertica;" |
| 266 |
f"Server={module.params['cluster']};" |
| 267 |
f"Port={module.params['port']};" |
| 268 |
f"Database={db};" |
| 269 |
f"User={module.params['login_user']};" |
| 270 |
f"Password={module.params['login_password']};" |
| 271 |
f"ConnectionLoadBalance=true" |
| 272 |
) |
| 273 |
db_conn = pyodbc.connect(dsn, autocommit=True) |
| 274 |
cursor = db_conn.cursor() |
| 275 |
except Exception as e: |
| 276 |
module.fail_json(msg=f"Unable to connect to database: {e}.", exception=traceback.format_exc()) |
| 277 |
|
| 278 |
try: |
| 279 |
schema_facts = get_schema_facts(cursor) |
| 280 |
user_facts = get_user_facts(cursor) |
| 281 |
role_facts = get_role_facts(cursor) |
| 282 |
configuration_facts = get_configuration_facts(cursor) |
| 283 |
node_facts = get_node_facts(cursor) |
| 284 |
|
| 285 |
module.exit_json( |
| 286 |
changed=False, |
| 287 |
vertica_schemas=schema_facts, |
| 288 |
vertica_users=user_facts, |
| 289 |
vertica_roles=role_facts, |
| 290 |
vertica_configuration=configuration_facts, |
| 291 |
vertica_nodes=node_facts, |
| 292 |
) |
| 293 |
except NotSupportedError as e: |
| 294 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 295 |
except SystemExit: |
| 296 |
# avoid catching this on python 2.4 |
| 297 |
raise |
| 298 |
except Exception as e: |
| 299 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 300 |
|
| 301 |
|
| 302 |
if __name__ == "__main__": |
| 303 |
main() |