Debian Codegraph @

ansible_collections/community/proxysql/plugins/modules/proxysql_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright: (c) 2021, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 DOCUMENTATION = '''
10 ---
11 module: proxysql_info
12 author: "Markus Bergholz (@markuman)"
13 short_description: Gathers information about proxysql server
14 description:
15 - Gathers information about proxysql server.
16 - Caution. The number of tables that returns, depends on the underlying proyxsql server version.
17 version_added: '1.2.0'
18 extends_documentation_fragment:
19 - community.proxysql.proxysql.connectivity
20 notes:
21 - Supports C(check_mode).
22 '''
23
24 EXAMPLES = '''
25 - name: Receive information about proxysql setup
26 community.proxysql.proxysql_info:
27 login_user: admin
28 login_password: admin
29 '''
30
31 RETURN = '''
32 stdout:
33 description: The number of tables that returns, depends on the underlying proyxsql server version.
34 returned: Always
35 type: dict
36 sample:
37 changed: false
38 failed: false
39 version:
40 description: Version of proxysql.
41 sample:
42 full: 2.1.1-40-g1c2b7e4
43 major: 2
44 minor: 1
45 release: 1
46 suffix: 40
47 type: dict
48 returned: Always
49 tables:
50 description: List of tables that exist in the requested proxysql version.
51 sample:
52 - global_variables
53 - mysql_aws_aurora_hostgroups
54 - mysql_collations
55 - mysql_firewall_whitelist_rules
56 - mysql_firewall_whitelist_sqli_fingerprints
57 - mysql_firewall_whitelist_users
58 - mysql_galera_hostgroups
59 - mysql_group_replication_hostgroups
60 - mysql_query_rules
61 - mysql_query_rules_fast_routing
62 - mysql_replication_hostgroups
63 - mysql_servers
64 - mysql_users
65 - proxysql_servers
66 - restapi_routes
67 - runtime_checksums_values
68 - runtime_global_variables
69 - runtime_mysql_aws_aurora_hostgroups
70 - runtime_mysql_firewall_whitelist_rules
71 - runtime_mysql_firewall_whitelist_sqli_fingerprints
72 - runtime_mysql_firewall_whitelist_users
73 - runtime_mysql_galera_hostgroups
74 - runtime_mysql_group_replication_hostgroups
75 - runtime_mysql_query_rules
76 - runtime_mysql_query_rules_fast_routing
77 - runtime_mysql_replication_hostgroups
78 - runtime_mysql_servers
79 - runtime_mysql_users
80 - runtime_proxysql_servers
81 - runtime_restapi_routes
82 - runtime_scheduler
83 - scheduler
84 type: list
85 returned: Always
86 global_variables:
87 description: Global variables of requested proxysql.
88 type: dict
89 returned: Always
90 '''
91
92 from ansible.module_utils.basic import AnsibleModule
93 from ansible_collections.community.proxysql.plugins.module_utils.mysql import (
94 mysql_connect,
95 mysql_driver,
96 proxysql_common_argument_spec,
97 )
98 from ansible.module_utils._text import to_native
99
100 # ===========================================
101 # proxysql module specific support methods.
102 #
103
104
105 def get_tables(cursor):
106 result = dict()
107 tables = list()
108
109 cursor.execute("show tables")
110
111 for table in cursor.fetchall():
112 tables.append(table.get('tables'))
113 result['tables'] = tables
114
115 for table in result.get('tables'):
116 cursor.execute("select * from {table}".format(table=table))
117
118 if 'global_variables' in table:
119 result[table] = dict()
120 for item in cursor.fetchall():
121 result[table][item.get('variable_name')] = item.get('variable_value')
122
123 else:
124 result[table] = cursor.fetchall()
125
126 return result
127
128
129 def main():
130 module = AnsibleModule(
131 supports_check_mode=True,
132 argument_spec=proxysql_common_argument_spec()
133 )
134
135 login_user = module.params["login_user"]
136 login_password = module.params["login_password"]
137 config_file = module.params["config_file"]
138
139 cursor = None
140 try:
141 cursor, db_conn, version = mysql_connect(
142 module,
143 login_user,
144 login_password,
145 config_file,
146 cursor_class='DictCursor'
147 )
148 except mysql_driver.Error as e:
149 module.fail_json(msg="unable to connect to ProxySQL Admin Module: %s" % to_native(e))
150
151 result = get_tables(cursor)
152
153 result['version'] = version
154
155 module.exit_json(**result)
156
157
158 if __name__ == '__main__':
159 main()