Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright: (c) 2017, 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_global_variables
12 author: "Ben Mildren (@bmildren)"
13 short_description: Gets or sets the proxysql global variables
14 description:
15 - The M(community.proxysql.proxysql_global_variables) module gets or sets the proxysql global
16 variables.
17 options:
18 variable:
19 description:
20 - Defines which variable should be returned, or if I(value) is specified
21 which variable should be updated.
22 type: str
23 required: true
24 value:
25 description:
26 - Defines a value the variable specified using I(variable) should be set
27 to.
28 type: str
29 extends_documentation_fragment:
30 - community.proxysql.proxysql.managing_config
31 - community.proxysql.proxysql.connectivity
32 notes:
33 - Supports C(check_mode).
34 '''
35
36 EXAMPLES = '''
37 ---
38 # This example sets the value of a variable, saves the mysql admin variables
39 # config to disk, and dynamically loads the mysql admin variables config to
40 # runtime. It uses supplied credentials to connect to the proxysql admin
41 # interface.
42
43 - name: Set the value of a variable
44 community.proxysql.proxysql_global_variables:
45 login_user: 'admin'
46 login_password: 'admin'
47 variable: 'mysql-max_connections'
48 value: 4096
49
50 # This example gets the value of a variable. It uses credentials in a
51 # supplied config file to connect to the proxysql admin interface.
52
53 - name: Get the value of a variable
54 community.proxysql.proxysql_global_variables:
55 config_file: '~/proxysql.cnf'
56 variable: 'mysql-default_query_delay'
57 '''
58
59 RETURN = '''
60 stdout:
61 description: Returns the mysql variable supplied with it's associated value.
62 returned: Returns the current variable and value, or the newly set value
63 for the variable supplied..
64 type: dict
65 "sample": {
66 "changed": false,
67 "msg": "The variable is already been set to the supplied value",
68 "var": {
69 "variable_name": "mysql-poll_timeout",
70 "variable_value": "3000"
71 }
72 }
73 '''
74
75 from ansible.module_utils.basic import AnsibleModule
76 from ansible_collections.community.proxysql.plugins.module_utils.mysql import (
77 mysql_connect,
78 mysql_driver,
79 proxysql_common_argument_spec,
80 save_config_to_disk,
81 load_config_to_runtime,
82 )
83 from ansible.module_utils._text import to_native
84
85 # ===========================================
86 # proxysql module specific support methods.
87 #
88
89
90 def check_config(variable, value, cursor):
91 query_string = \
92 """SELECT count(*) AS `variable_count`
93 FROM global_variables
94 WHERE variable_name = %s and variable_value = %s"""
95
96 query_data = \
97 [variable, value]
98
99 cursor.execute(query_string, query_data)
100 check_count = cursor.fetchone()
101
102 if isinstance(check_count, tuple):
103 return int(check_count[0]) > 0
104
105 return (int(check_count['variable_count']) > 0)
106
107
108 def get_config(variable, cursor):
109
110 query_string = \
111 """SELECT *
112 FROM global_variables
113 WHERE variable_name = %s"""
114
115 query_data = \
116 [variable, ]
117
118 cursor.execute(query_string, query_data)
119 row_count = cursor.rowcount
120 resultset = cursor.fetchone()
121
122 if row_count > 0:
123 return resultset
124 else:
125 return False
126
127
128 def set_config(variable, value, cursor):
129
130 query_string = \
131 """UPDATE global_variables
132 SET variable_value = %s
133 WHERE variable_name = %s"""
134
135 query_data = \
136 [value, variable]
137
138 cursor.execute(query_string, query_data)
139 return True
140
141
142 def manage_config(variable, save_to_disk, load_to_runtime, cursor, state):
143 if state:
144 if save_to_disk:
145 save_config_to_disk(cursor, "VARIABLES", variable)
146 if load_to_runtime:
147 load_config_to_runtime(cursor, "VARIABLES", variable)
148
149 # ===========================================
150 # Module execution.
151 #
152
153
154 def main():
155 argument_spec = proxysql_common_argument_spec()
156 argument_spec.update(
157 variable=dict(required=True, type='str'),
158 value=dict(),
159 save_to_disk=dict(default=True, type='bool'),
160 load_to_runtime=dict(default=True, type='bool')
161 )
162
163 module = AnsibleModule(
164 supports_check_mode=True,
165 argument_spec=argument_spec
166 )
167
168 login_user = module.params["login_user"]
169 login_password = module.params["login_password"]
170 config_file = module.params["config_file"]
171 variable = module.params["variable"]
172 value = module.params["value"]
173 save_to_disk = module.params["save_to_disk"]
174 load_to_runtime = module.params["load_to_runtime"]
175
176 cursor = None
177 try:
178 cursor, db_conn, version = mysql_connect(module,
179 login_user,
180 login_password,
181 config_file,
182 cursor_class='DictCursor')
183 except mysql_driver.Error as e:
184 module.fail_json(
185 msg="unable to connect to ProxySQL Admin Module.. %s" % to_native(e)
186 )
187
188 result = {}
189
190 if not value:
191 try:
192 if get_config(variable, cursor):
193 result['changed'] = False
194 result['msg'] = \
195 "Returned the variable and it's current value"
196 result['var'] = get_config(variable, cursor)
197 else:
198 module.fail_json(
199 msg="The variable \"%s\" was not found" % variable
200 )
201
202 except mysql_driver.Error as e:
203 module.fail_json(
204 msg="unable to get config.. %s" % to_native(e)
205 )
206 else:
207 try:
208 if get_config(variable, cursor):
209 if not check_config(variable, value, cursor):
210 if not module.check_mode:
211 result['changed'] = set_config(variable, value, cursor)
212 result['msg'] = \
213 "Set the variable to the supplied value"
214 result['var'] = get_config(variable, cursor)
215 manage_config(variable,
216 save_to_disk,
217 load_to_runtime,
218 cursor,
219 result['changed'])
220 else:
221 result['changed'] = True
222 result['msg'] = ("Variable would have been set to" +
223 " the supplied value, however" +
224 " check_mode is enabled.")
225 else:
226 result['changed'] = False
227 result['msg'] = ("The variable is already been set to" +
228 " the supplied value")
229 result['var'] = get_config(variable, cursor)
230 else:
231 module.fail_json(
232 msg="The variable \"%s\" was not found" % variable
233 )
234
235 except mysql_driver.Error as e:
236 module.fail_json(
237 msg="unable to set config.. %s" % to_native(e)
238 )
239
240 module.exit_json(**result)
241
242
243 if __name__ == '__main__':
244 main()