diff --git a/README.md b/README.md index 6d88f75..3cb5f16 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,10 @@ Whether MySQL should be enabled on startup. Whether the global my.cnf should be overwritten each time this role is run. Setting this to `no` tells Ansible to only create the `my.cnf` file if it doesn't exist. This should be left at its default value (`yes`) if you'd like to use this role's variables to configure MySQL. + mysql_config_include_files: [] + +A list of files that should override the default global my.cnf. Each item in the array requires a "src" parameter which is a path to a file. An optional "force" parameter can force the file to be updated each time ansible runs. + mysql_databases: [] The MySQL databases to create. A database has the values `name`, `encoding` (defaults to `utf8`), `collation` (defaults to `utf8_general_ci`) and `replicate` (defaults to `1`, only used if replication is configured). The formats of these are the same as in the `mysql_db` module. diff --git a/defaults/main.yml b/defaults/main.yml index 5f5742b..5987d8e 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -70,6 +70,11 @@ mysql_log: "" mysql_log_error: /var/log/mysql.err mysql_syslog_tag: mysql +mysql_config_include_files: [] +# - src: path/relative/to/playbook/file.cnf +# - { src: path/relative/to/playbook/anotherfile.cnf, force: yes } + + # Databases. mysql_databases: [] # Full example: diff --git a/tasks/configure.yml b/tasks/configure.yml index a913f3a..493152a 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -9,6 +9,26 @@ force: "{{ overwrite_global_mycnf }}" notify: restart mysql +- name: Verify mysql include directory exists. + file: + path: "{{ mysql_config_include_dir }}" + state: directory + owner: root + group: root + mode: 0755 + when: mysql_config_include_files | length + +- name: Copy my.cnf override files into include directory. + template: + src: "{{ item.src }}" + dest: "{{ mysql_config_include_dir }}/{{ item.src | basename }}" + owner: root + group: root + mode: 0644 + force: "{{ item.force | default(False) }}" + with_items: mysql_config_include_files + notify: restart mysql + - name: Create slow query log file (if configured). shell: "touch {{ mysql_slow_query_log_file }} creates={{ mysql_slow_query_log_file }}" when: mysql_slow_query_log_enabled diff --git a/templates/my.cnf.j2 b/templates/my.cnf.j2 index 99c4e8d..ed9c766 100644 --- a/templates/my.cnf.j2 +++ b/templates/my.cnf.j2 @@ -96,3 +96,11 @@ max_allowed_packet = {{ mysql_mysqldump_max_allowed_packet }} [mysqld_safe] pid-file = {{ mysql_pid_file }} + +{% if mysql_config_include_files | length %} +# * IMPORTANT: Additional settings that can override those from this file! +# The files must end with '.cnf', otherwise they'll be ignored. +# +!includedir {{ mysql_config_include_dir }} +{% endif %} + diff --git a/vars/Debian.yml b/vars/Debian.yml index 55c88e8..097be99 100644 --- a/vars/Debian.yml +++ b/vars/Debian.yml @@ -5,4 +5,5 @@ __mysql_packages: - mysql-server __mysql_slow_query_log_file: /var/log/mysql/mysql-slow.log mysql_config_file: /etc/mysql/my.cnf +mysql_config_include_dir: /etc/mysql/conf.d mysql_socket: /var/run/mysqld/mysqld.sock diff --git a/vars/RedHat.yml b/vars/RedHat.yml index cc483d1..71117a2 100644 --- a/vars/RedHat.yml +++ b/vars/RedHat.yml @@ -5,4 +5,5 @@ __mysql_packages: - mysql-server __mysql_slow_query_log_file: /var/log/mysql-slow.log mysql_config_file: /etc/my.cnf +mysql_config_include_dir: /etc/my.cnf.d mysql_socket: /var/lib/mysql/mysql.sock