Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,21 @@ mysql_replication_master_inventory_host: "{{ mysql_replication_master }}"
mysql_replication_user: []

mysql_hide_passwords: false

# mysql_daemon has historically meant three things at once: the systemd unit,
# the client binary, and the engine flavour. That is coherent wherever the
# three share a name -- MariaDB everywhere, and MySQL on Debian, where the
# unit really is `mysql`.
#
# It is not coherent for MySQL on Enterprise Linux, where the unit is `mysqld`
# and the client is `mysql`. No single value works there:
#
# mariadb wrong on all three (the EL default, so MySQL-on-EL fails outright)
# mysqld correct unit, but runs the SERVER binary with client flags and
# matches neither flavour test
# mysql correct client and flavour, wrong unit
#
# These default to mysql_daemon, so nothing changes for any existing caller.
# Set them only where the three genuinely differ.
mysql_client: "{{ mysql_daemon }}"
mysql_flavour: "{{ mysql_daemon }}"
2 changes: 1 addition & 1 deletion tasks/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- name: Get MySQL version
block:
- name: Run mysql --version
ansible.builtin.command: '{{ mysql_daemon }} --version'
ansible.builtin.command: '{{ mysql_client }} --version'
register: mysql_cli_version
changed_when: false
check_mode: false
Expand Down
26 changes: 13 additions & 13 deletions tasks/secure-installation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
and (mysql_install_packages | bool or mysql_user_password_update)

- name: Disallow root login remotely
ansible.builtin.command: '{{ mysql_daemon }} -NBe "{{ item }}"'
ansible.builtin.command: '{{ mysql_client }} -NBe "{{ item }}"'
with_items:
- DELETE FROM mysql.user WHERE User='{{ mysql_root_username }}' AND Host NOT IN ('localhost', '127.0.0.1', '::1')
changed_when: false

- name: Get list of hosts for the root user.
ansible.builtin.command: >
{{ mysql_daemon }} -NBe
{{ mysql_client }} -NBe
"SELECT Host
FROM mysql.user
WHERE User = '{{ mysql_root_username }}'
Expand All @@ -44,48 +44,48 @@
# Set root password for MySQL >= 8.4 and MariaDB ≥ 10.4
- name: Update MySQL root authentication via socket for localhost (Linux, MySQL ≥ 8.4)
ansible.builtin.shell: >
{{ mysql_daemon }} -u root -NBe
{{ mysql_client }} -u root -NBe
"ALTER USER '{{ mysql_root_username }}'@'{{ item }}'
IDENTIFIED {{ (mysql_daemon == 'mariadb') | ternary('VIA unix_socket', 'WITH auth_socket') }}; FLUSH PRIVILEGES;"
IDENTIFIED {{ (mysql_flavour == 'mariadb') | ternary('VIA unix_socket', 'WITH auth_socket') }}; FLUSH PRIVILEGES;"
no_log: "{{ mysql_hide_passwords }}"
with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
when:
- (mysql_install_packages | bool) or mysql_root_password_update
- ansible_facts.system == "Linux"
- (mysql_cli_version is version('8.0.34', '>=') and mysql_daemon == 'mysql') or
(mysql_cli_version is version('10.4', '>=') and mysql_daemon == 'mariadb')
- (mysql_cli_version is version('8.0.34', '>=') and mysql_flavour == 'mysql') or
(mysql_cli_version is version('10.4', '>=') and mysql_flavour == 'mariadb')

# Note: We do not use mysql_user for this operation, as it doesn't always update
# the root password correctly. See: https://goo.gl/MSOejW
# Set root password for 5.7.x. ≤ MySQL < 8.4 and MariaDB ≥ 10.4
- name: Update MySQL root password for localhost root account (5.7.x ≤ MySQL < 8.4)
ansible.builtin.shell: >
{{ mysql_daemon }} -u root -NBe
{{ mysql_client }} -u root -NBe
"ALTER USER '{{ mysql_root_username }}'@'{{ item }}'
IDENTIFIED {{ (mysql_daemon == 'mariadb') | ternary('VIA', 'WITH') }} mysql_native_password
IDENTIFIED {{ (mysql_flavour == 'mariadb') | ternary('VIA', 'WITH') }} mysql_native_password
BY '{{ mysql_root_password }}'; FLUSH PRIVILEGES;"
no_log: "{{ mysql_hide_passwords }}"
with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
when:
- (mysql_install_packages | bool) or mysql_root_password_update
- (
mysql_daemon == 'mysql' and (
mysql_flavour == 'mysql' and (
mysql_cli_version is version('5.7', '>=') and
mysql_cli_version is version('8.4', '<')
)
) or
(mysql_daemon == 'mariadb' and mysql_cli_version is version('10.4', '<'))
(mysql_flavour == 'mariadb' and mysql_cli_version is version('10.4', '<'))

# Set root password for MySQL < 5.7.x.
- name: Update MySQL root password for localhost root account (< 5.7.x).
ansible.builtin.shell: >
{{ mysql_daemon }} -NBe
{{ mysql_client }} -NBe
'SET PASSWORD FOR "{{ mysql_root_username }}"@"{{ item }}" = PASSWORD("{{ mysql_root_password }}"); FLUSH PRIVILEGES;'
no_log: "{{ mysql_hide_passwords }}"
with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
when:
- (mysql_install_packages | bool) or mysql_root_password_update
- (mysql_daemon == 'mysql' and mysql_cli_version is version('5.7', '<'))
- (mysql_flavour == 'mysql' and mysql_cli_version is version('5.7', '<'))

# Has to be after the root password assignment, for idempotency.
- name: Copy .my.cnf file with root password credentials.
Expand All @@ -102,7 +102,7 @@

- name: Get list of hosts for the anonymous user.
ansible.builtin.command: >
{{ mysql_daemon }} -NBe "SELECT Host FROM mysql.user WHERE User = ''"
{{ mysql_client }} -NBe "SELECT Host FROM mysql.user WHERE User = ''"
register: mysql_anonymous_hosts
changed_when: false
check_mode: false
Expand Down
Loading