Skip to content

Commit 87b62fc

Browse files
author
Colin Hoglund
authored
Merge pull request #1 from singleplatform-eng/merge_from_upstream
Merge from upstream
2 parents f0f322d + e65433b commit 87b62fc

18 files changed

+190
-60
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.retry
2+
tests/test.sh

.travis.yml

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,52 @@ services: docker
33

44
env:
55
- distro: centos7
6-
init: /usr/lib/systemd/systemd
7-
run_opts: "--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
86
playbook: centos-7-test.yml
97
- distro: centos6
10-
init: /sbin/init
11-
run_opts: ""
8+
playbook: test.yml
9+
- distro: debian8
1210
playbook: test.yml
1311
- distro: ubuntu1604
14-
init: /lib/systemd/systemd
15-
run_opts: "--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
1612
playbook: test.yml
1713
- distro: ubuntu1404
18-
init: /sbin/init
19-
run_opts: ""
2014
playbook: test.yml
2115

22-
services:
23-
- docker
24-
25-
before_install:
26-
# Pull container
27-
- 'docker pull geerlingguy/docker-${distro}-ansible:latest'
28-
2916
script:
30-
- container_id=$(mktemp)
31-
# Run container in detached state.
32-
- 'docker run --detach --volume="${PWD}":/etc/ansible/roles/role_under_test:ro ${run_opts} geerlingguy/docker-${distro}-ansible:latest "${init}" > "${container_id}"'
17+
# Configure test script so we can run extra tests after playbook is run.
18+
- export container_id=$(date +%s)
19+
- export cleanup=false
3320

34-
# Ansible syntax check.
35-
- 'docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/${playbook} --syntax-check'
21+
# Download test shim.
22+
- wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/
23+
- chmod +x ${PWD}/tests/test.sh
3624

37-
# Test role.
38-
- 'docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/roles/role_under_test/tests/${playbook}'
39-
40-
# Test role idempotence.
41-
- idempotence=$(mktemp)
42-
- docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/roles/role_under_test/tests/${playbook} | tee -a ${idempotence}
43-
- >
44-
tail ${idempotence}
45-
| grep -q 'changed=0.*failed=0'
46-
&& (echo 'Idempotence test: pass' && exit 0)
47-
|| (echo 'Idempotence test: fail' && exit 1)
25+
# Run tests.
26+
- ${PWD}/tests/test.sh
4827

4928
# Some MySQL debugging (show all the logs).
50-
- docker exec --tty "$(cat ${container_id})" env TERM=xterm ls -lah /var/log
51-
- docker exec --tty "$(cat ${container_id})" env TERM=xterm cat /var/log/mysql/error.log || true
52-
- docker exec --tty "$(cat ${container_id})" env TERM=xterm cat /var/log/mysql.err || true
29+
- docker exec --tty ${container_id} env TERM=xterm ls -lah /var/log
30+
- docker exec --tty ${container_id} env TERM=xterm cat /var/log/mysql/error.log || true
31+
- docker exec --tty ${container_id} env TERM=xterm cat /var/log/mysql.err || true
5332

5433
# Check to make sure we can connect to MySQL via Unix socket.
5534
- >
56-
sudo docker exec "$(cat ${container_id})" mysql -u root -proot -e 'show databases;'
35+
sudo docker exec ${container_id} mysql -u root -proot -e 'show databases;'
5736
| grep -q 'information_schema'
5837
&& (echo 'MySQL running normally' && exit 0)
5938
|| (echo 'MySQL not running' && exit 1)
6039
6140
# Check to make sure we can connect to MySQL via TCP.
6241
- >
63-
sudo docker exec "$(cat ${container_id})" mysql -u root -proot -h 127.0.0.1 -e 'show databases;'
42+
sudo docker exec ${container_id} mysql -u root -proot -h 127.0.0.1 -e 'show databases;'
6443
| grep -q 'information_schema'
6544
&& (echo 'MySQL running normally' && exit 0)
6645
|| (echo 'MySQL not running' && exit 1)
6746
6847
after_failure:
6948
# Check MySQL settings.
70-
- 'docker exec --tty "$(cat ${container_id})" env TERM=xterm cat /var/log/mysql/error.log'
71-
- 'docker exec --tty "$(cat ${container_id})" env TERM=xterm cat /var/log/mysql.err'
72-
- 'docker exec --tty "$(cat ${container_id})" env TERM=xterm cat /var/log/mysql.log'
49+
- 'docker exec --tty ${container_id} env TERM=xterm cat /var/log/mysql/error.log'
50+
- 'docker exec --tty ${container_id} env TERM=xterm cat /var/log/mysql.err'
51+
- 'docker exec --tty ${container_id} env TERM=xterm cat /var/log/mysql.log'
7352

7453
notifications:
7554
webhooks: https://galaxy.ansible.com/api/v1/notifications/

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Jeff Geerling
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,25 @@ 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

3034
Whether to force update the MySQL root user's password. By default, this role will only change the root user's password when MySQL is first configured. You can force an update by setting this to `yes`.
3135

3236
> Note: If you get an error like `ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)` after a failed or interrupted playbook run, this usually means the root password wasn't originally updated to begin with. Try either removing the `.my.cnf` file inside the configured `mysql_user_home` or updating it and setting `password=''` (the insecure default password). Run the playbook again, with `mysql_root_password_update` set to `yes`, and the setup should complete.
3337
38+
> Note: If you get an error like `ERROR 1698 (28000): Access denied for user 'root'@'localhost' (using password: YES)` when trying to log in from the CLI you might need to run as root or sudoer.
39+
3440
mysql_enabled_on_startup: yes
3541

3642
Whether MySQL should be enabled on startup.
@@ -103,6 +109,29 @@ The rest of the settings in `defaults/main.yml` control MySQL's memory usage and
103109

104110
Replication settings. Set `mysql_server_id` and `mysql_replication_role` by server (e.g. the master would be ID `1`, with the `mysql_replication_role` of `master`, and the slave would be ID `2`, with the `mysql_replication_role` of `slave`). The `mysql_replication_user` uses the same keys as `mysql_users`, and is created on master servers, and used to replicate on all the slaves.
105111

112+
### Later versions of MySQL on CentOS 7
113+
114+
If you want to install MySQL from the official repository instead of installing the system default MariaDB equivalents, you can add the following `pre_tasks` task in your playbook:
115+
116+
```yaml
117+
pre_tasks:
118+
- name: Install the MySQL repo.
119+
yum:
120+
name: http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
121+
state: present
122+
when: ansible_os_family == "RedHat"
123+
124+
- name: Override variables for MySQL (RedHat).
125+
set_fact:
126+
mysql_daemon: mysqld
127+
mysql_packages: ['mysql-server']
128+
mysql_log_error: /var/log/mysqld.err
129+
mysql_syslog_tag: mysqld
130+
mysql_pid_file: /var/run/mysqld/mysqld.pid
131+
mysql_socket: /var/lib/mysql/mysql.sock
132+
when: ansible_os_family == "RedHat"
133+
```
134+
106135
### MariaDB usage
107136
108137
This role works with either MySQL or a compatible version of MariaDB. On RHEL/CentOS 7+, the mariadb database engine was substituted as the default MySQL replacement package. No modifications are necessary though all of the variables still reference 'mysql' instead of mariadb.
@@ -148,4 +177,4 @@ MIT / BSD
148177

149178
## Author Information
150179

151-
This role was created in 2014 by [Jeff Geerling](http://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/).
180+
This role was created in 2014 by [Jeff Geerling](https://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/).

defaults/main.yml

Lines changed: 14 additions & 1 deletion
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

@@ -31,6 +39,7 @@ mysql_port: "3306"
3139
mysql_bind_address: '0.0.0.0'
3240
mysql_skip_name_resolve: no
3341
mysql_datadir: /var/lib/mysql
42+
mysql_sql_mode: ''
3443
# The following variables have a default value depending on operating system.
3544
# mysql_pid_file: /var/run/mysqld/mysqld.pid
3645
# mysql_socket: /var/lib/mysql/mysql.sock
@@ -50,15 +59,19 @@ mysql_read_buffer_size: "1M"
5059
mysql_read_rnd_buffer_size: "4M"
5160
mysql_myisam_sort_buffer_size: "64M"
5261
mysql_thread_cache_size: "8"
62+
mysql_query_cache_type: "0"
5363
mysql_query_cache_size: "16M"
5464
mysql_query_cache_limit: "1M"
5565
mysql_max_connections: "151"
5666
mysql_tmp_table_size: "16M"
5767
mysql_max_heap_table_size: "16M"
68+
mysql_group_concat_max_len: "1024"
69+
mysql_join_buffer_size: "262144"
5870

5971
# Other settings.
6072
mysql_lower_case_table_names: "0"
6173
mysql_wait_timeout: "28800"
74+
mysql_event_scheduler_state: "OFF"
6275

6376
# InnoDB settings.
6477
mysql_innodb_file_per_table: "1"
@@ -80,7 +93,7 @@ mysql_mysqldump_max_allowed_packet: "64M"
8093
# Logging settings.
8194
mysql_log: ""
8295
# The following variables have a default value depending on operating system.
83-
# mysql_log_error: /var/log/mysql.err
96+
# mysql_log_error: /var/log/mysql/mysql.err
8497
# mysql_syslog_tag: mysql
8598

8699
mysql_config_include_files: []

meta/main.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ galaxy_info:
66
description: MySQL server for RHEL/CentOS and Debian/Ubuntu.
77
company: "Midwestern Mac, LLC"
88
license: "license (BSD, MIT)"
9-
min_ansible_version: 1.9
9+
min_ansible_version: 2.2
1010
platforms:
1111
- name: EL
1212
versions:
@@ -18,5 +18,8 @@ galaxy_info:
1818
- name: Debian
1919
versions:
2020
- all
21+
- name: Archlinux
22+
versions:
23+
- all
2124
galaxy_tags:
2225
- database

tasks/configure.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
notify: restart mysql
3131

3232
- name: Create slow query log file (if configured).
33-
shell: "touch {{ mysql_slow_query_log_file }} creates={{ mysql_slow_query_log_file }}"
33+
command: "touch {{ mysql_slow_query_log_file }}"
34+
args:
35+
creates: "{{ mysql_slow_query_log_file }}"
36+
warn: no
3437
when: mysql_slow_query_log_enabled
3538

3639
- name: Create datadir if it does not exist
@@ -52,7 +55,10 @@
5255
when: mysql_slow_query_log_enabled
5356

5457
- name: Create error log file (if configured).
55-
shell: "touch {{ mysql_log_error }} creates={{ mysql_log_error }}"
58+
command: "touch {{ mysql_log_error }}"
59+
args:
60+
creates: "{{ mysql_log_error }}"
61+
warn: no
5662
when: mysql_log == "" and mysql_log_error != ""
5763

5864
- name: Set ownership on error log file (if configured).

tasks/main.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@
1111
when: ansible_os_family == 'Debian'
1212
static: no
1313

14+
- include: setup-Archlinux.yml
15+
when: ansible_os_family == 'Archlinux'
16+
static: no
17+
1418
- name: Check if MySQL packages were installed.
1519
set_fact:
16-
mysql_install_packages: "{{ (rh_mysql_install_packages is defined and rh_mysql_install_packages.changed) or (deb_mysql_install_packages is defined and deb_mysql_install_packages.changed) }}"
20+
mysql_install_packages: "{{ (rh_mysql_install_packages is defined and rh_mysql_install_packages.changed)
21+
or (deb_mysql_install_packages is defined and deb_mysql_install_packages.changed)
22+
or (arch_mysql_install_packages is defined and arch_mysql_install_packages.changed) }}"
1723

1824
# Configure MySQL.
1925
- include: configure.yml

tasks/replication.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
name: "{{ mysql_replication_user.name }}"
55
host: "{{ mysql_replication_user.host | default('%') }}"
66
password: "{{ mysql_replication_user.password }}"
7-
priv: "{{ mysql_replication_user.priv | default('*.*:REPLICATION SLAVE') }}"
7+
priv: "{{ mysql_replication_user.priv | default('*.*:REPLICATION SLAVE,REPLICATION CLIENT') }}"
88
state: present
99
when: >
1010
(mysql_replication_role == 'master')
1111
and mysql_replication_user
1212
and (mysql_replication_master != '')
1313
1414
- name: Check slave replication status.
15-
mysql_replication: mode=getslave
15+
mysql_replication:
16+
mode: getslave
17+
login_user: "{{ mysql_replication_user.name }}"
18+
login_password: "{{ mysql_replication_user.password }}"
1619
ignore_errors: true
1720
register: slave
1821
when: >

tasks/secure-installation.yml

Lines changed: 27 additions & 7 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
17-
always_run: true
35+
check_mode: no
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,25 +48,26 @@
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 = ""'
4868
register: mysql_anonymous_hosts
4969
changed_when: false
50-
always_run: true
70+
check_mode: no
5171

5272
- name: Remove anonymous MySQL users.
5373
mysql_user:

0 commit comments

Comments
 (0)