|
1 | 1 | ---
|
| 2 | +- name: Get MySQL version. |
| 3 | + command: 'mysql --version' |
| 4 | + register: mysql_cli_version |
| 5 | + changed_when: false |
| 6 | + |
2 | 7 | - name: Disallow root login remotely
|
3 | 8 | command: 'mysql -NBe "{{ item }}"'
|
4 | 9 | with_items:
|
5 | 10 | - DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')
|
6 |
| - changed_when: False |
| 11 | + changed_when: false |
7 | 12 |
|
8 | 13 | - name: Get list of hosts for the root user.
|
9 | 14 | command: mysql -NBe 'SELECT Host FROM mysql.user WHERE User = "root" ORDER BY (Host="localhost") ASC'
|
|
13 | 18 |
|
14 | 19 | # Note: We do not use mysql_user for this operation, as it doesn't always update
|
15 | 20 | # the root password correctly. See: https://goo.gl/MSOejW
|
16 |
| -- name: Update MySQL root password for localhost root account. |
| 21 | +# Set root password for MySQL >= 5.7.x. |
| 22 | +- name: Update MySQL root password for localhost root account (5.7.x). |
| 23 | + shell: > |
| 24 | + mysql -u root -NBe |
| 25 | + 'ALTER USER "{{ mysql_root_username }}"@"{{ item }}" IDENTIFIED WITH mysql_native_password BY "{{ mysql_root_password }}";' |
| 26 | + with_items: "{{ mysql_root_hosts.stdout_lines }}" |
| 27 | + when: ((mysql_install_packages | bool) or mysql_root_password_update) and ('5.7.' in mysql_cli_version.stdout) |
| 28 | + |
| 29 | +# Set root password for MySQL < 5.7.x. |
| 30 | +- name: Update MySQL root password for localhost root account (< 5.7.x). |
17 | 31 | shell: >
|
18 | 32 | mysql -u root -NBe
|
19 | 33 | 'SET PASSWORD FOR "{{ mysql_root_username }}"@"{{ item }}" = PASSWORD("{{ mysql_root_password }}");'
|
20 | 34 | with_items: "{{ mysql_root_hosts.stdout_lines }}"
|
21 |
| - when: mysql_install_packages | bool or mysql_root_password_update |
| 35 | + when: ((mysql_install_packages | bool) or mysql_root_password_update) and ('5.7.' not in mysql_cli_version.stdout) |
22 | 36 |
|
23 | 37 | # Has to be after the root password assignment, for idempotency.
|
24 | 38 | - name: Copy .my.cnf file with root password credentials.
|
|
0 commit comments