Skip to content

Commit 6912cbd

Browse files
authored
Merge pull request geerlingguy#188 from geerlingguy/BlackMesh-run_as_non_root
More correctly and automatically support running the role as a user other than root
2 parents 5e70f6e + 32824ab commit 6912cbd

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ No special requirements; note that this role requires root access, so either run
1818
Available variables are listed below, along with default values (see `defaults/main.yml`):
1919

2020
mysql_user_home: /root
21+
mysql_user_name: root
22+
mysql_user_password: root
2123

22-
The home directory inside which Python MySQL settings will be stored, which Ansible will use when connecting to MySQL. This should be the home directory of the user which runs this Ansible role.
24+
The home directory inside which Python MySQL settings will be stored, which Ansible will use when connecting to MySQL. This should be the home directory of the user which runs this Ansible role. The `mysql_user_name` and `mysql_user_password` can be set if you are running this role under a non-root user account and want to set a non-root user.
2325

26+
mysql_root_home: /root
27+
mysql_root_username: root
2428
mysql_root_password: root
2529

26-
The MySQL root user account password.
30+
The MySQL root user account details.
2731

2832
mysql_root_password_update: no
2933

defaults/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
---
2+
# Set this to the user ansible is logging in as - should have root
3+
# or sudo access
24
mysql_user_home: /root
5+
mysql_user_name: root
6+
mysql_user_password: root
7+
8+
# The default root user installed by mysql - almost always root
9+
mysql_root_home: /root
310
mysql_root_username: root
411
mysql_root_password: root
512

613
# Set this to `yes` to forcibly update the root password.
714
mysql_root_password_update: no
15+
mysql_user_password_update: no
816

917
mysql_enabled_on_startup: yes
1018

tasks/secure-installation.yml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,36 @@
44
register: mysql_cli_version
55
changed_when: false
66

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+
725
- name: Disallow root login remotely
826
command: 'mysql -NBe "{{ item }}"'
927
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')
1129
changed_when: false
1230

1331
- 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"
1533
register: mysql_root_hosts
1634
changed_when: false
1735
always_run: true
36+
when: mysql_install_packages | bool or mysql_root_password_update
1837

1938
# Note: We do not use mysql_user for this operation, as it doesn't always update
2039
# the root password correctly. See: https://goo.gl/MSOejW
@@ -29,19 +48,20 @@
2948
# Set root password for MySQL < 5.7.x.
3049
- name: Update MySQL root password for localhost root account (< 5.7.x).
3150
shell: >
32-
mysql -u root -NBe
51+
mysql -NBe
3352
'SET PASSWORD FOR "{{ mysql_root_username }}"@"{{ item }}" = PASSWORD("{{ mysql_root_password }}");'
3453
with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
3554
when: ((mysql_install_packages | bool) or mysql_root_password_update) and ('5.7.' not in mysql_cli_version.stdout)
3655

3756
# Has to be after the root password assignment, for idempotency.
3857
- name: Copy .my.cnf file with root password credentials.
3958
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"
4261
owner: root
4362
group: root
4463
mode: 0600
64+
when: mysql_install_packages | bool or mysql_root_password_update
4565

4666
- name: Get list of hosts for the anonymous user.
4767
command: mysql -NBe 'SELECT Host FROM mysql.user WHERE User = ""'

templates/root-my.cnf.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[client]
2+
user={{ mysql_root_username }}
3+
password={{ mysql_root_password }}

templates/user-my.cnf.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[client]
2-
user={{ mysql_root_username }}
3-
password="{{ mysql_root_password }}"
2+
user={{ mysql_user_name }}
3+
password={{ mysql_user_password }}

0 commit comments

Comments
 (0)