|
4 | 4 | register: mysql_cli_version
|
5 | 5 | changed_when: false
|
6 | 6 |
|
| 7 | +- name: Ensure default user is present. |
| 8 | + mysql_user: |
| 9 | + name: "{{ mysql_user_name }}" |
| 10 | + host: 'localhost' |
| 11 | + password: "{{ mysql_user_password }}" |
| 12 | + priv: '*.*:ALL,GRANT' |
| 13 | + state: present |
| 14 | + when: mysql_user_name != mysql_root_username |
| 15 | + |
| 16 | +# Has to be after the password assignment, for idempotency. |
| 17 | +- name: Copy user-my.cnf file with password credentials. |
| 18 | + template: |
| 19 | + src: "user-my.cnf.j2" |
| 20 | + dest: "{{ mysql_user_home }}/.my.cnf" |
| 21 | + owner: "{{ mysql_user_name }}" |
| 22 | + mode: 0600 |
| 23 | + when: mysql_user_name != mysql_root_username and (mysql_install_packages | bool or mysql_user_password_update) |
| 24 | + |
7 | 25 | - name: Disallow root login remotely
|
8 | 26 | command: 'mysql -NBe "{{ item }}"'
|
9 | 27 | with_items:
|
10 |
| - - DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1') |
| 28 | + - DELETE FROM mysql.user WHERE User='{{ mysql_root_username }}' AND Host NOT IN ('localhost', '127.0.0.1', '::1') |
11 | 29 | changed_when: false
|
12 | 30 |
|
13 | 31 | - name: Get list of hosts for the root user.
|
14 |
| - command: mysql -NBe 'SELECT Host FROM mysql.user WHERE User = "root" ORDER BY (Host="localhost") ASC' |
| 32 | + command: mysql -NBe "SELECT Host FROM mysql.user WHERE User = '{{ mysql_root_username }}' ORDER BY (Host='localhost') ASC" |
15 | 33 | register: mysql_root_hosts
|
16 | 34 | changed_when: false
|
17 | 35 | always_run: true
|
| 36 | + when: mysql_install_packages | bool or mysql_root_password_update |
18 | 37 |
|
19 | 38 | # Note: We do not use mysql_user for this operation, as it doesn't always update
|
20 | 39 | # the root password correctly. See: https://goo.gl/MSOejW
|
|
29 | 48 | # Set root password for MySQL < 5.7.x.
|
30 | 49 | - name: Update MySQL root password for localhost root account (< 5.7.x).
|
31 | 50 | shell: >
|
32 |
| - mysql -u root -NBe |
| 51 | + mysql -NBe |
33 | 52 | 'SET PASSWORD FOR "{{ mysql_root_username }}"@"{{ item }}" = PASSWORD("{{ mysql_root_password }}");'
|
34 | 53 | with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
|
35 | 54 | when: ((mysql_install_packages | bool) or mysql_root_password_update) and ('5.7.' not in mysql_cli_version.stdout)
|
36 | 55 |
|
37 | 56 | # Has to be after the root password assignment, for idempotency.
|
38 | 57 | - name: Copy .my.cnf file with root password credentials.
|
39 | 58 | template:
|
40 |
| - src: "user-my.cnf.j2" |
41 |
| - dest: "{{ mysql_user_home }}/.my.cnf" |
| 59 | + src: "root-my.cnf.j2" |
| 60 | + dest: "{{ mysql_root_home }}/.my.cnf" |
42 | 61 | owner: root
|
43 | 62 | group: root
|
44 | 63 | mode: 0600
|
| 64 | + when: mysql_install_packages | bool or mysql_root_password_update |
45 | 65 |
|
46 | 66 | - name: Get list of hosts for the anonymous user.
|
47 | 67 | command: mysql -NBe 'SELECT Host FROM mysql.user WHERE User = ""'
|
|
0 commit comments