1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/modules/vertica_configuration.py

205 lines
6.4 KiB
Python
Raw Normal View History

2020-03-09 10:11:07 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
2020-03-09 10:11:07 +01:00
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
---
module: vertica_configuration
short_description: Updates Vertica configuration parameters
2020-03-09 10:11:07 +01:00
description:
- Updates Vertica configuration parameters.
extends_documentation_fragment:
- community.general.attributes
attributes:
check_mode:
support: full
diff_mode:
support: none
2020-03-09 10:11:07 +01:00
options:
Tidy up validations + bug fixes + deprecations (#1830) * fixed validation-modules for plugins/modules/database/misc/elasticsearch_plugin.py * fixed validation-modules for plugins/modules/database/misc/kibana_plugin.py * fixed validation-modules for plugins/modules/database/misc/riak.py * fixed validation-modules for plugins/modules/database/vertica/vertica_info.py * fixed validation-modules for plugins/modules/database/vertica/vertica_role.py * fixed validation-modules for plugins/modules/database/vertica/vertica_schema.py * fixed validation-modules for plugins/modules/database/vertica/vertica_user.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_domain.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_host_ports.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_host.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_pool.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_vol.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_vol_map.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_host_ports.py * fixed validation-modules for plugins/modules/system/runit.py * fixed validation-modules for plugins/modules/source_control/bzr.py * fixed validation-modules for plugins/modules/source_control/hg.py * fixed validation-modules for plugins/modules/storage/emc/emc_vnx_sg_member.py * fixed validation-modules for plugins/modules/identity/opendj/opendj_backendprop.py * fixed validation-modules for plugins/modules/files/iso_extract.py * fixed validation-modules for plugins/modules/monitoring/logstash_plugin.py * fixed validation-modules for plugins/modules/database/aerospike/aerospike_migrations.py * Tidy up a number of sanity checks for some modules * added changelog fragment * Some parameters in vertica_* had their aliases documented as the name, and sometimes vice-versa as well * Adjustments per PR * Rolled back sanity ignores for runit * Update changelogs/fragments/1830-valmod_docmissingtype_batch1.yml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-02-16 09:27:24 +01:00
parameter:
2020-03-09 10:11:07 +01:00
description:
- Name of the parameter to update.
required: true
Tidy up validations + bug fixes + deprecations (#1830) * fixed validation-modules for plugins/modules/database/misc/elasticsearch_plugin.py * fixed validation-modules for plugins/modules/database/misc/kibana_plugin.py * fixed validation-modules for plugins/modules/database/misc/riak.py * fixed validation-modules for plugins/modules/database/vertica/vertica_info.py * fixed validation-modules for plugins/modules/database/vertica/vertica_role.py * fixed validation-modules for plugins/modules/database/vertica/vertica_schema.py * fixed validation-modules for plugins/modules/database/vertica/vertica_user.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_domain.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_host_ports.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_host.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_pool.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_vol.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_vol_map.py * fixed validation-modules for plugins/modules/storage/ibm/ibm_sa_host_ports.py * fixed validation-modules for plugins/modules/system/runit.py * fixed validation-modules for plugins/modules/source_control/bzr.py * fixed validation-modules for plugins/modules/source_control/hg.py * fixed validation-modules for plugins/modules/storage/emc/emc_vnx_sg_member.py * fixed validation-modules for plugins/modules/identity/opendj/opendj_backendprop.py * fixed validation-modules for plugins/modules/files/iso_extract.py * fixed validation-modules for plugins/modules/monitoring/logstash_plugin.py * fixed validation-modules for plugins/modules/database/aerospike/aerospike_migrations.py * Tidy up a number of sanity checks for some modules * added changelog fragment * Some parameters in vertica_* had their aliases documented as the name, and sometimes vice-versa as well * Adjustments per PR * Rolled back sanity ignores for runit * Update changelogs/fragments/1830-valmod_docmissingtype_batch1.yml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2021-02-16 09:27:24 +01:00
aliases: [name]
type: str
2020-03-09 10:11:07 +01:00
value:
description:
- Value of the parameter to be set.
type: str
2020-03-09 10:11:07 +01:00
db:
description:
- Name of the Vertica database.
type: str
2020-03-09 10:11:07 +01:00
cluster:
description:
- Name of the Vertica cluster.
default: localhost
type: str
2020-03-09 10:11:07 +01:00
port:
description:
- Vertica cluster port to connect to.
default: '5433'
type: str
2020-03-09 10:11:07 +01:00
login_user:
description:
- The username used to authenticate with.
default: dbadmin
type: str
2020-03-09 10:11:07 +01:00
login_password:
description:
- The password used to authenticate with.
type: str
2020-03-09 10:11:07 +01:00
notes:
- The default authentication assumes that you are either logging in as or sudo'ing
to the C(dbadmin) account on the host.
- This module uses C(pyodbc), a Python ODBC database adapter. You must ensure
that C(unixODBC) and C(pyodbc) is installed on the host and properly configured.
- Configuring C(unixODBC) for Vertica requires C(Driver = /opt/vertica/lib64/libverticaodbc.so)
to be added to the C(Vertica) section of either C(/etc/odbcinst.ini) or C($HOME/.odbcinst.ini)
and both C(ErrorMessagesPath = /opt/vertica/lib64) and C(DriverManagerEncoding = UTF-16)
to be added to the C(Driver) section of either C(/etc/vertica.ini) or C($HOME/.vertica.ini).
requirements: [ 'unixODBC', 'pyodbc' ]
author: "Dariusz Owczarek (@dareko)"
'''
EXAMPLES = """
2020-05-16 15:07:51 +02:00
- name: Updating load_balance_policy
community.general.vertica_configuration: name=failovertostandbyafter value='8 hours'
2020-03-09 10:11:07 +01:00
"""
import traceback
PYODBC_IMP_ERR = None
try:
import pyodbc
except ImportError:
PYODBC_IMP_ERR = traceback.format_exc()
pyodbc_found = False
else:
pyodbc_found = True
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.common.text.converters import to_native
2020-03-09 10:11:07 +01:00
class NotSupportedError(Exception):
pass
class CannotDropError(Exception):
pass
# module specific functions
def get_configuration_facts(cursor, parameter_name=''):
facts = {}
cursor.execute("""
select c.parameter_name, c.current_value, c.default_value
from configuration_parameters c
where c.node_name = 'ALL'
and (? = '' or c.parameter_name ilike ?)
""", parameter_name, parameter_name)
while True:
rows = cursor.fetchmany(100)
if not rows:
break
for row in rows:
facts[row.parameter_name.lower()] = {
'parameter_name': row.parameter_name,
'current_value': row.current_value,
'default_value': row.default_value}
return facts
def check(configuration_facts, parameter_name, current_value):
parameter_key = parameter_name.lower()
if current_value and current_value.lower() != configuration_facts[parameter_key]['current_value'].lower():
return False
return True
def present(configuration_facts, cursor, parameter_name, current_value):
parameter_key = parameter_name.lower()
changed = False
if current_value and current_value.lower() != configuration_facts[parameter_key]['current_value'].lower():
cursor.execute("select set_config_parameter('{0}', '{1}')".format(parameter_name, current_value))
changed = True
if changed:
configuration_facts.update(get_configuration_facts(cursor, parameter_name))
return changed
# module logic
def main():
module = AnsibleModule(
argument_spec=dict(
parameter=dict(required=True, aliases=['name']),
value=dict(default=None),
db=dict(default=None),
cluster=dict(default='localhost'),
port=dict(default='5433'),
login_user=dict(default='dbadmin'),
login_password=dict(default=None, no_log=True),
), supports_check_mode=True)
if not pyodbc_found:
module.fail_json(msg=missing_required_lib('pyodbc'), exception=PYODBC_IMP_ERR)
parameter_name = module.params['parameter']
current_value = module.params['value']
db = ''
if module.params['db']:
db = module.params['db']
changed = False
try:
dsn = (
"Driver=Vertica;"
"Server={0};"
"Port={1};"
"Database={2};"
"User={3};"
"Password={4};"
"ConnectionLoadBalance={5}"
).format(module.params['cluster'], module.params['port'], db,
module.params['login_user'], module.params['login_password'], 'true')
db_conn = pyodbc.connect(dsn, autocommit=True)
cursor = db_conn.cursor()
except Exception as e:
module.fail_json(msg="Unable to connect to database: {0}.".format(to_native(e)),
exception=traceback.format_exc())
try:
configuration_facts = get_configuration_facts(cursor)
if module.check_mode:
changed = not check(configuration_facts, parameter_name, current_value)
else:
try:
changed = present(configuration_facts, cursor, parameter_name, current_value)
except pyodbc.Error as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
except NotSupportedError as e:
module.fail_json(msg=to_native(e), ansible_facts={'vertica_configuration': configuration_facts})
except CannotDropError as e:
module.fail_json(msg=to_native(e), ansible_facts={'vertica_configuration': configuration_facts})
except SystemExit:
# avoid catching this on python 2.4
raise
except Exception as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
module.exit_json(changed=changed, parameter=parameter_name, ansible_facts={'vertica_configuration': configuration_facts})
if __name__ == '__main__':
main()