Git initial commit
This commit is contained in:
commit
f2971a52d3
9
LICENSE
Executable file
9
LICENSE
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) <year> <copyright holders>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
3
README.md
Executable file
3
README.md
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
# cloud-mariadb
|
||||||
|
|
||||||
|
Ansible role to provide MariaDB for tools that rely on it.
|
||||||
32
defaults/main.yml
Executable file
32
defaults/main.yml
Executable file
@ -0,0 +1,32 @@
|
|||||||
|
---
|
||||||
|
cloud_apps: /opt
|
||||||
|
cloud_storage: /opt/storage
|
||||||
|
cloud_stage: prod
|
||||||
|
cloud_update: false
|
||||||
|
|
||||||
|
mariadb_version_major: 11
|
||||||
|
mariadb_version_minor: 4
|
||||||
|
mariadb_version_patch: 2
|
||||||
|
mariadb_version: "{{ mariadb_version_major }}.{{ mariadb_version_minor }}.{{ mariadb_version_patch }}"
|
||||||
|
|
||||||
|
#mariadb root pass; defaults to empty
|
||||||
|
mariadb_root_pass:
|
||||||
|
|
||||||
|
mariadb_storage_folder: "{{ cloud_storage }}/mariadb/{{ mariadb_version_major }}"
|
||||||
|
mariadb_backup_folder: "{{ cloud_storage }}/mariadb/backup"
|
||||||
|
mariadb_backup_fileprefix: "{{ mariadb_backup_folder }}.mariadb.{{ mariadb_version_major }}.backup"
|
||||||
|
mariadb_local_sock: "{{ mariadb_storage_folder }}/mariadb.sock"
|
||||||
|
mariadb_config_location: "/etc/mysql"
|
||||||
|
mariadb_config_file: "mariadb.cnf"
|
||||||
|
|
||||||
|
mariadb_port: 3306
|
||||||
|
mariadb_remote_login: false
|
||||||
|
mariadb_skip_networking: 0
|
||||||
|
|
||||||
|
db_configs:
|
||||||
|
- type: mariadb
|
||||||
|
name: db_name
|
||||||
|
user: db_user
|
||||||
|
pass: db_user_password
|
||||||
|
priv: ALL
|
||||||
|
user_host: localhost
|
||||||
7
handlers/main.yml
Executable file
7
handlers/main.yml
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
- name: restart mariadb service
|
||||||
|
service:
|
||||||
|
name: mariadb
|
||||||
|
state: restarted
|
||||||
|
enabled: yes
|
||||||
|
when: not mdb_install.changed
|
||||||
16
meta/main.yml
Executable file
16
meta/main.yml
Executable file
@ -0,0 +1,16 @@
|
|||||||
|
---
|
||||||
|
galaxy_info:
|
||||||
|
role_name: mariadb
|
||||||
|
namespace: hahn-cloud
|
||||||
|
author: Lars Hahn
|
||||||
|
company: Data Learning
|
||||||
|
license: MIT
|
||||||
|
description: Role to setup mariadb with ansible.
|
||||||
|
min_ansible_version: 2.8
|
||||||
|
platforms:
|
||||||
|
- name: Debian
|
||||||
|
versions:
|
||||||
|
- 10
|
||||||
|
galaxy_tags:
|
||||||
|
- mariadb
|
||||||
|
dependencies: []
|
||||||
106
tasks/main.yml
Executable file
106
tasks/main.yml
Executable file
@ -0,0 +1,106 @@
|
|||||||
|
---
|
||||||
|
- name: install requirements for mariadb
|
||||||
|
apt:
|
||||||
|
update_cache: yes
|
||||||
|
state: "{% if cloud_update | bool %}latest{% else %}present{% endif %}"
|
||||||
|
install_recommends: yes
|
||||||
|
pkg:
|
||||||
|
- apt-transport-https
|
||||||
|
- software-properties-common
|
||||||
|
- gpg
|
||||||
|
- gpg-agent
|
||||||
|
|
||||||
|
- name: install mariadb repository key
|
||||||
|
apt_key:
|
||||||
|
url: https://mariadb.org/mariadb_release_signing_key.asc
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: install mariadb repository
|
||||||
|
apt_repository:
|
||||||
|
repo: "deb [arch={{ ansible_kernel.split('-')[-1] }}] https://archive.mariadb.org/mariadb-{{ mariadb_version }}/repo/debian/ {{ ansible_distribution_release }} main"
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: setup mariadb config path
|
||||||
|
file:
|
||||||
|
state: directory
|
||||||
|
path: "{{ mariadb_config_location }}"
|
||||||
|
when: mariadb_config_location != "/etc"
|
||||||
|
|
||||||
|
- name: configure mariadb
|
||||||
|
template:
|
||||||
|
mode: 0644
|
||||||
|
src: "etc/mariadb.cnf.j2"
|
||||||
|
dest: "{{ mariadb_config_location }}/{{ mariadb_config_file }}"
|
||||||
|
owner: root
|
||||||
|
notify: restart mariadb service
|
||||||
|
|
||||||
|
- name: install mariadb
|
||||||
|
apt:
|
||||||
|
update_cache: yes
|
||||||
|
state: "{% if cloud_update | bool %}latest{% else %}present{% endif %}"
|
||||||
|
install_recommends: yes
|
||||||
|
pkg:
|
||||||
|
- mariadb-server
|
||||||
|
- python3-pymysql
|
||||||
|
register: mdb_install
|
||||||
|
|
||||||
|
- name: create mariadb storage
|
||||||
|
file:
|
||||||
|
state: directory
|
||||||
|
path: "{{ item }}"
|
||||||
|
owner: root
|
||||||
|
group: mysql
|
||||||
|
mode: 0770
|
||||||
|
loop:
|
||||||
|
- "{{ mariadb_storage_folder }}"
|
||||||
|
- "{{ mariadb_backup_folder}}"
|
||||||
|
|
||||||
|
- name: setup mariadb service
|
||||||
|
service:
|
||||||
|
name: mariadb
|
||||||
|
enabled: true
|
||||||
|
state: started
|
||||||
|
|
||||||
|
# This should only run when initially installed
|
||||||
|
- name: initial setup mariadb root user
|
||||||
|
mysql_user:
|
||||||
|
check_implicit_admin: yes
|
||||||
|
name: "{{ mariadb_root_user }}"
|
||||||
|
password: "{{ mariadb_root_pass }}"
|
||||||
|
priv: '*.*:ALL,GRANT'
|
||||||
|
login_unix_socket: "{{ mariadb_local_sock }}"
|
||||||
|
when: mdb_install.changed
|
||||||
|
|
||||||
|
- name: setup sql secrets file for root
|
||||||
|
template:
|
||||||
|
mode: 0600
|
||||||
|
src: root/.my.cnf.j2
|
||||||
|
dest: /root/.my.cnf
|
||||||
|
|
||||||
|
- name: setup initial cleanup script
|
||||||
|
template:
|
||||||
|
mode: 0600
|
||||||
|
src: root/secure_install.sql.j2
|
||||||
|
dest: /root/secure_install.sql
|
||||||
|
|
||||||
|
- name: run initial cleanup
|
||||||
|
shell: |
|
||||||
|
mysql
|
||||||
|
--defaults-extra-file /root/mdb_local.cnf
|
||||||
|
--no-auto-rehash
|
||||||
|
< /root/secure_install.sql
|
||||||
|
when: mdb_install.changed
|
||||||
|
|
||||||
|
- name: remove all anonymous user accounts
|
||||||
|
mysql_user:
|
||||||
|
name: ""
|
||||||
|
host_all: yes
|
||||||
|
state: absent
|
||||||
|
login_unix_socket: "{{ mariadb_local_sock }}"
|
||||||
|
|
||||||
|
- name: Setup databases based on mariadb conf
|
||||||
|
include_tasks: setup-db.yml
|
||||||
|
loop: "{{ db_configs | json_query('[?type==`mariadb`]') }}"
|
||||||
|
loop_control:
|
||||||
|
loop_var: db
|
||||||
|
label: "{% if 'dbname' in db %}{{ db.dbname }}{% elif 'dbuser' in db %}{{ db.dbuser }}{% else %}::pass_redacted::{% endif %}"
|
||||||
38
tasks/setup-db.yml
Executable file
38
tasks/setup-db.yml
Executable file
@ -0,0 +1,38 @@
|
|||||||
|
---
|
||||||
|
- name: set DB related config options
|
||||||
|
set_fact:
|
||||||
|
local_db_name: "{{ db.name if 'name' in db else 'noentry' }}"
|
||||||
|
local_db_user: "{{ db.user if 'user' in db else 'noentry' }}"
|
||||||
|
local_db_pass: "{{ db.pass if 'pass' in db else 'noentry' }}"
|
||||||
|
local_db_user_prives: "{{ db.priv if 'priv' in db else 'ALL' }}"
|
||||||
|
local_db_user_host: "{{ db.user_host if 'user_host' in db else 'localhost'}}"
|
||||||
|
|
||||||
|
- name: "create {{ local_db_user }} user"
|
||||||
|
mysql_user:
|
||||||
|
name: "{{ local_db_user }}"
|
||||||
|
password: "{{ local_db_pass }}"
|
||||||
|
host: "{{ local_db_user_host }}"
|
||||||
|
state: present
|
||||||
|
login_unix_socket: "{{ mariadb_local_sock }}"
|
||||||
|
when:
|
||||||
|
- local_db_user != 'noentry'
|
||||||
|
- local_db_pass != 'noentry'
|
||||||
|
|
||||||
|
- name: "setup {{ local_db_name }} database"
|
||||||
|
mysql_db:
|
||||||
|
name: "{{ local_db_name }}"
|
||||||
|
state: present
|
||||||
|
login_unix_socket: "{{ mariadb_local_sock }}"
|
||||||
|
when:
|
||||||
|
- local_db_name != 'noentry'
|
||||||
|
|
||||||
|
- name: "Grant privs '{{ local_db_user_prives }}' for user '{{ local_db_user }}' to database '{{ local_db_name }}'"
|
||||||
|
mysql_user:
|
||||||
|
append_privs: yes
|
||||||
|
name: "{{ local_db_user }}"
|
||||||
|
host: "{{ local_db_user_host }}"
|
||||||
|
priv: "{{local_db_name}}.*:{{ local_db_user_prives }}"
|
||||||
|
login_unix_socket: "{{ mariadb_local_sock }}"
|
||||||
|
when:
|
||||||
|
- local_db_user != 'noentry'
|
||||||
|
- local_db_name != 'noentry'
|
||||||
11
templates/etc/mariadb.cnf.j2
Normal file
11
templates/etc/mariadb.cnf.j2
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[mysqld]
|
||||||
|
datadir={{ mariadb_storage_folder }}
|
||||||
|
socket={{ mariadb_local_sock }}
|
||||||
|
skip_networking={{ mariadb_skip_networking }}
|
||||||
|
{% if not mariadb_remote_login %}
|
||||||
|
bind-address = 127.0.0.1
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
[client]
|
||||||
|
port={{ mariadb_port }}
|
||||||
|
socket={{ mariadb_local_sock }}
|
||||||
3
templates/root/.my.cnf.j2
Executable file
3
templates/root/.my.cnf.j2
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
[client]
|
||||||
|
user={{ mariadb_root_user }}
|
||||||
|
password={{ mariadb_root_pass }}
|
||||||
4
templates/root/secure_install.sql.j2
Executable file
4
templates/root/secure_install.sql.j2
Executable file
@ -0,0 +1,4 @@
|
|||||||
|
DELETE FROM mysql.user WHERE User='{{ mariadb_root_user }}' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
|
||||||
|
DROP DATABASE IF EXISTS test;
|
||||||
|
DELETE FROM mysql.db WHERE Db='test' OR Db='test_%'
|
||||||
|
FLUSH PRIVILEGES;
|
||||||
0
templates/storage/backup.sh.j2
Normal file
0
templates/storage/backup.sh.j2
Normal file
2
vars/main.yml
Executable file
2
vars/main.yml
Executable file
@ -0,0 +1,2 @@
|
|||||||
|
---
|
||||||
|
mariadb_root_user: "root"
|
||||||
Loading…
Reference in New Issue
Block a user