Skip to content

Commit

Permalink
mysql_variables: fix boolean value handling (#653)
Browse files Browse the repository at this point in the history
* mysql_variables: fix boolean value handling

* fix

* Fix tests

* Fix tests

* Fix

* Fix

* Fix

* Fix comment
  • Loading branch information
Andersson007 committed Jun 28, 2024
1 parent 33e8754 commit 4912f1a
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/2-mysql_variables.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- mysql_variables - fix the module always changes on boolean values (https://github.com/ansible-collections/community.mysql/issues/652).
21 changes: 21 additions & 0 deletions plugins/modules/mysql_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
value:
description:
- If set, then sets variable value to this.
- With boolean values, use C(0)|C(1) or quoted C("ON")|C("OFF").
type: str
mode:
description:
Expand Down Expand Up @@ -74,6 +75,11 @@
variable: read_only
value: 1
mode: persist
- name: Set a boolean using ON/OFF notation
mysql_variables:
variable: log_slow_replica_statements
value: "ON" # Make sure it's quoted
'''

RETURN = r'''
Expand Down Expand Up @@ -176,6 +182,18 @@ def setvariable(cursor, mysqlvar, value, mode='global'):
return result


def convert_bool_setting_value_wanted(val):
"""Converts passed value from 0,1,on,off to ON/OFF
as it's represented in the server.
"""
if val in ('on', 1):
val = 'ON'
elif val in ('off', 0):
val = 'OFF'

return val


def main():
argument_spec = mysql_common_argument_spec()
argument_spec.update(
Expand Down Expand Up @@ -243,6 +261,9 @@ def main():
# Type values before using them
value_wanted = typedvalue(value)
value_actual = typedvalue(mysqlvar_val)
if value_actual in ('ON', 'OFF') and value_wanted not in ('ON', 'OFF'):
value_wanted = convert_bool_setting_value_wanted(value_wanted)

value_in_auto_cnf = None
if var_in_mysqld_auto_cnf is not None:
value_in_auto_cnf = typedvalue(var_in_mysqld_auto_cnf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,99 @@
var_name: "{{set_name}}"
var_value: '{{set_value}}'

#=========================================================================
# Bugfix https://github.com/ansible-collections/community.mysql/issues/652

- name: Get server version
register: result
mysql_info:
<<: *mysql_params

- name: Set variable name when running on MySQL
set_fact:
log_slow_statements: log_slow_replica_statements
when: result.server_engine == 'MySQL'

- name: Set variable name when running on MariaDB
set_fact:
log_slow_statements: log_slow_slave_statements
when: result.server_engine == 'MariaDB'

- name: Set a boolean value using ON
mysql_variables:
<<: *mysql_params
variable: "{{ log_slow_statements }}"
value: "ON"
register: result

- name: Check that it changed
assert:
that:
- result is changed or result.msg == "Variable is already set to requested value."
- result.msg == "Variable is already set to requested value." or result.queries == ["SET GLOBAL `{{ log_slow_statements }}` = ON"]

- name: Set a boolean value again using ON
mysql_variables:
<<: *mysql_params
variable: "{{ log_slow_statements }}"
value: "ON"
register: result

- name: Check that it didn't change
assert:
that:
- result is not changed

- name: Set a boolean value again using 1
mysql_variables:
<<: *mysql_params
variable: "{{ log_slow_statements }}"
value: 1
register: result

- name: Check that it didn't change
assert:
that:
- result is not changed

- name: Set a boolean value using OFF
mysql_variables:
<<: *mysql_params
variable: "{{ log_slow_statements }}"
value: "OFF"
register: result

- name: Check that it changed
assert:
that:
- result is changed
- result.queries == ["SET GLOBAL `{{ log_slow_statements }}` = OFF"]

- name: Set a boolean value again using 0
mysql_variables:
<<: *mysql_params
variable: "{{ log_slow_statements }}"
value: 0
register: result

- name: Check that it didn't change
assert:
that:
- result is not changed

- name: Set a boolean value using on
mysql_variables:
<<: *mysql_params
variable: "{{ log_slow_statements }}"
value: "on"
register: result

- name: Check that it changed
assert:
that:
- result is changed
- result.queries == ["SET GLOBAL `{{ log_slow_statements }}` = ON"]

#============================================================
# Verify mysql_variable fails with an incorrect login_password parameter
#
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/plugins/modules/test_mysql_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import pytest

from ansible_collections.community.mysql.plugins.modules.mysql_variables import (
convert_bool_setting_value_wanted,
)


@pytest.mark.parametrize(
'value,output',
[
(1, 'ON'),
(0, 'OFF'),
(2, 2),
('on', 'ON'),
('off', 'OFF'),
('ON', 'ON'),
('OFF', 'OFF'),
]
)
def test_convert_bool_value(value, output):
assert convert_bool_setting_value_wanted(value) == output

0 comments on commit 4912f1a

Please sign in to comment.