From 19f403eb4ca71f637b76c212162fa03dd41499d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20HORTA?= Date: Thu, 10 Oct 2024 16:26:04 +0200 Subject: [PATCH] Allow mysql_config_include_files to contain raw content In some cases, specifying a known src path in `mysql_config_include_files` is not possible / complicated and it may be much cleaner to provide raw file content to be sent. In that case, we need to use the `ansible.builtin.copy` module. We can extend the current implementation by simply filtering on whether `src` or `content` is set in each item in `mysql_config_include_files` --- README.md | 2 +- defaults/main.yml | 3 +++ tasks/configure.yml | 19 +++++++++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dfd9354a..446ae0da 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ Whether the global my.cnf should be overwritten each time this role is run. Sett 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. +A list of files that should override the default global my.cnf. Each item in the array requires either a "src" parameter which is a path to a file, or both a "content" (file contents) and a "dst" (file name) parameter. An optional "force" parameter can force the file to be updated each time ansible runs. ```yaml mysql_databases: [] diff --git a/defaults/main.yml b/defaults/main.yml index 01eeb3c3..61d14f79 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -107,6 +107,9 @@ mysql_log: "" mysql_config_include_files: [] # - src: path/relative/to/playbook/file.cnf # - { src: path/relative/to/playbook/anotherfile.cnf, force: true } +# - content: | +# # some included configuration here +# dst: yetanotherfile.cnf # Databases. mysql_databases: [] diff --git a/tasks/configure.yml b/tasks/configure.yml index 50714c14..4fa570e9 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -31,7 +31,7 @@ mode: 0755 when: mysql_config_include_files | length -- name: Copy my.cnf override files into include directory. +- name: Template my.cnf override files into include directory. ansible.builtin.template: src: "{{ item.src }}" dest: "{{ mysql_config_include_dir }}/{{ item.src | basename }}" @@ -39,7 +39,22 @@ group: root mode: 0644 force: "{{ item.force | default(false) }}" - with_items: "{{ mysql_config_include_files }}" + with_items: "{{ mysql_config_include_files | selectattr('src', 'defined') }}" + loop_control: + label: "{{ item.src | basename }}" + notify: restart mysql + +- name: Copy my.cnf override files into include directory. + ansible.builtin.copy: + content: "{{ item.content }}" + dest: "{{ mysql_config_include_dir }}/{{ item.dst }}" + owner: root + group: root + mode: 0644 + force: "{{ item.force | default(false) }}" + with_items: "{{ mysql_config_include_files | selectattr('content', 'defined') }}" + loop_control: + label: "{{ item.dst }}" notify: restart mysql - name: Create slow query log file (if configured).