diff --git a/changelogs/fragments/742-mysql_role-sql_log_bin.yml b/changelogs/fragments/742-mysql_role-sql_log_bin.yml new file mode 100644 index 0000000..2fd7f41 --- /dev/null +++ b/changelogs/fragments/742-mysql_role-sql_log_bin.yml @@ -0,0 +1,2 @@ +minor_changes: + - mysql_role - add ``sql_log_bin`` option to disable binary logging for the connection (https://github.com/ansible-collections/community.mysql/issues/742). diff --git a/plugins/modules/mysql_role.py b/plugins/modules/mysql_role.py index eb474dc..b7880e3 100644 --- a/plugins/modules/mysql_role.py +++ b/plugins/modules/mysql_role.py @@ -133,6 +133,13 @@ options: default: true version_added: '3.8.0' + sql_log_bin: + description: + - Whether binary logging should be enabled or disabled for the connection. + type: bool + default: true + version_added: '4.1.0' + notes: - Roles are supported since MySQL 8.0.0 and MariaDB 10.0.5. - Pay attention that the module runs C(SET DEFAULT ROLE ALL TO) @@ -299,6 +306,13 @@ EXAMPLES = r''' members: - 'existing_user@localhost' - 'not_existing_user@localhost' + +- name: Create role without binary logging + community.mysql.mysql_role: + name: readers + state: present + priv: 'fiction.*:SELECT' + sql_log_bin: false ''' RETURN = '''#''' @@ -975,6 +989,7 @@ def main(): set_default_role_all=dict(type='bool', default=True), members_must_exist=dict(type='bool', default=True), column_case_sensitive=dict(type='bool', default=True), + sql_log_bin=dict(type='bool', default=True), ) module = AnsibleModule( argument_spec=argument_spec, @@ -1010,6 +1025,7 @@ def main(): set_default_role_all = module.params['set_default_role_all'] members_must_exist = module.params['members_must_exist'] column_case_sensitive = module.params['column_case_sensitive'] + sql_log_bin = module.params['sql_log_bin'] if priv and not isinstance(priv, (str, dict)): msg = ('The "priv" parameter must be str or dict ' @@ -1047,6 +1063,9 @@ def main(): 'are correct or %s has the credentials. ' 'Exception message: %s' % (config_file, to_native(e))) + if not sql_log_bin: + cursor.execute("SET SQL_LOG_BIN=0;") + # Set defaults changed = False diff --git a/tests/integration/targets/test_mysql_role/tasks/main.yml b/tests/integration/targets/test_mysql_role/tasks/main.yml index 44e3308..cb43963 100644 --- a/tests/integration/targets/test_mysql_role/tasks/main.yml +++ b/tests/integration/targets/test_mysql_role/tasks/main.yml @@ -22,3 +22,10 @@ - name: Test column case sensitive ansible.builtin.import_tasks: file: test_column_case_sensitive.yml + +# Test sql_log_bin parameter +# (https://github.com/ansible-collections/community.mysql/issues/742) +- name: Test sql_log_bin parameter + ansible.builtin.include_tasks: test_sql_log_bin.yml + when: + - db_engine == 'mysql' diff --git a/tests/integration/targets/test_mysql_role/tasks/test_sql_log_bin.yml b/tests/integration/targets/test_mysql_role/tasks/test_sql_log_bin.yml new file mode 100644 index 0000000..7b8fe22 --- /dev/null +++ b/tests/integration/targets/test_mysql_role/tasks/test_sql_log_bin.yml @@ -0,0 +1,86 @@ +--- +# Test sql_log_bin parameter for mysql_role module +# https://github.com/ansible-collections/community.mysql/issues/742 + +- name: Sql_log_bin | Set show_master_status variable + ansible.builtin.set_fact: + show_master_status: >- + {% if db_engine == 'mysql' and db_version is version('8.4', '>=') %} + SHOW BINARY LOG STATUS + {% else %} + SHOW MASTER STATUS + {% endif %} + +# ============================================================ +# Test sql_log_bin: true (default behavior - binlog events should be written) +# ============================================================ +- name: Sql_log_bin | Capture binlog position before creating role with sql_log_bin enabled + ansible.builtin.command: "{{ mysql_command }} -e \"{{ show_master_status }}\\G\"" + register: bin_log_position_1 + +- name: Sql_log_bin | Create role with sql_log_bin enabled + community.mysql.mysql_role: + login_user: '{{ mysql_user }}' + login_password: '{{ mysql_password }}' + login_host: '{{ mysql_host }}' + login_port: '{{ mysql_primary_port }}' + name: 'test_role_bin_on' + sql_log_bin: true + state: present + +- name: Sql_log_bin | Capture binlog position after creating role with sql_log_bin enabled + ansible.builtin.command: "{{ mysql_command }} -e \"{{ show_master_status }}\\G\"" + register: bin_log_position_2 + failed_when: bin_log_position_1.stdout_lines[2] == bin_log_position_2.stdout_lines[2] + +- name: Sql_log_bin | Remove role with sql_log_bin enabled + community.mysql.mysql_role: + login_user: '{{ mysql_user }}' + login_password: '{{ mysql_password }}' + login_host: '{{ mysql_host }}' + login_port: '{{ mysql_primary_port }}' + name: 'test_role_bin_on' + sql_log_bin: true + state: absent + +- name: Sql_log_bin | Capture binlog position after removing role with sql_log_bin enabled + ansible.builtin.command: "{{ mysql_command }} -e \"{{ show_master_status }}\\G\"" + register: bin_log_position_3 + failed_when: bin_log_position_2.stdout_lines[2] == bin_log_position_3.stdout_lines[2] + +# ============================================================ +# Test sql_log_bin: false (binlog events should NOT be written) +# ============================================================ +- name: Sql_log_bin | Capture binlog position before creating role with sql_log_bin disabled + ansible.builtin.command: "{{ mysql_command }} -e \"{{ show_master_status }}\\G\"" + register: bin_log_position_4 + +- name: Sql_log_bin | Create role with sql_log_bin disabled + community.mysql.mysql_role: + login_user: '{{ mysql_user }}' + login_password: '{{ mysql_password }}' + login_host: '{{ mysql_host }}' + login_port: '{{ mysql_primary_port }}' + name: 'test_role_bin_off' + sql_log_bin: false + state: present + +- name: Sql_log_bin | Capture binlog position after creating role with sql_log_bin disabled + ansible.builtin.command: "{{ mysql_command }} -e \"{{ show_master_status }}\\G\"" + register: bin_log_position_5 + failed_when: bin_log_position_4.stdout_lines[2] != bin_log_position_5.stdout_lines[2] + +- name: Sql_log_bin | Remove role with sql_log_bin disabled + community.mysql.mysql_role: + login_user: '{{ mysql_user }}' + login_password: '{{ mysql_password }}' + login_host: '{{ mysql_host }}' + login_port: '{{ mysql_primary_port }}' + name: 'test_role_bin_off' + sql_log_bin: false + state: absent + +- name: Sql_log_bin | Capture binlog position after removing role with sql_log_bin disabled + ansible.builtin.command: "{{ mysql_command }} -e \"{{ show_master_status }}\\G\"" + register: bin_log_position_6 + failed_when: bin_log_position_5.stdout_lines[2] != bin_log_position_6.stdout_lines[2]