From 3e982b9729e77179c587db64b61f939e3e0219e1 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Tue, 30 Aug 2022 07:48:38 -0500 Subject: [PATCH] add mysql role --- roles/mysql/README.md | 38 +++++++++++++++++++++ roles/mysql/defaults/main.yaml | 57 +++++++++++++++++++++++++++++++ roles/mysql/handlers/main.yml | 5 +++ roles/mysql/meta/main.yml | 60 +++++++++++++++++++++++++++++++++ roles/mysql/tasks/main.yml | 39 +++++++++++++++++++++ roles/mysql/templates/my.cnf.j2 | 12 +++++++ roles/mysql/tests/inventory | 2 ++ roles/mysql/tests/test.yml | 5 +++ roles/mysql/vars/Debian.yaml | 2 ++ roles/mysql/vars/Ubuntu-18.yaml | 5 +++ roles/mysql/vars/Ubuntu.yaml | 4 +++ roles/mysql/vars/main.yml | 2 ++ 12 files changed, 231 insertions(+) create mode 100644 roles/mysql/README.md create mode 100644 roles/mysql/defaults/main.yaml create mode 100644 roles/mysql/handlers/main.yml create mode 100644 roles/mysql/meta/main.yml create mode 100644 roles/mysql/tasks/main.yml create mode 100644 roles/mysql/templates/my.cnf.j2 create mode 100644 roles/mysql/tests/inventory create mode 100644 roles/mysql/tests/test.yml create mode 100644 roles/mysql/vars/Debian.yaml create mode 100644 roles/mysql/vars/Ubuntu-18.yaml create mode 100644 roles/mysql/vars/Ubuntu.yaml create mode 100644 roles/mysql/vars/main.yml diff --git a/roles/mysql/README.md b/roles/mysql/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/roles/mysql/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/roles/mysql/defaults/main.yaml b/roles/mysql/defaults/main.yaml new file mode 100644 index 0000000..cb49329 --- /dev/null +++ b/roles/mysql/defaults/main.yaml @@ -0,0 +1,57 @@ +--- +mysql_package_state: 'present' + +mysql_service_name: 'mysql' +mysql_service_state: 'started' +mysql_service_enabled: yes + +mysql_initialize_log_error: /var/tmp/mysqld_initialize.log + +mysql_cfg_path: /etc/my.cnf + +mysql_datadir_owner: mysql +mysql_datadir_group: mysql +mysql_datadir_mode: 0700 + +mysql_config: + mysql: + port: 3306 + socket: /var/run/mysqld/mysqld.sock + mysqld: + basedir: /usr + bind_address: 127.0.0.1 + datadir: /var/lib/mysql + default_storage_engine: InnoDB + innodb_buffer_pool_size: "{{ (ansible_memtotal_mb * 0.25) | int }}M" + innodb_file_per_table: 1 + innodb_flush_log_at_trx_commit: 1 + innodb_flush_method: O_DIRECT + innodb_log_file_size: 128M + innodb_log_files_in_group: 2 + key_buffer_size: 16M + log_error: /var/log/mysql/mysql-error.log + log_queries_not_using_indexes: 1 + max_allowed_packet: 16M + max_connect_errors: 1000000 + max_connections: 100 + max_heap_table_size: 32M + myisam_recover_options: FORCE,BACKUP + open_files_limit: 65535 + pid_file: /var/run/mysqld/mysqld.pid + query_cache_size: 0 + query_cache_type: 0 + slow_query_log: 1 + slow_query_log_file: /var/log/mysql/mysql-slow.log + socket: /var/run/mysqld/mysqld.sock + table_definition_cache: 4096 + table_open_cache: 300 + thread_cache_size: 16 + tmp_table_size: 32M + tmpdir: /tmp + user: mysql + mysqld_safe: + nice: 0 + socket: /var/run/mysqld/mysqld.sock + syslog: ~ + +# vim:ft=yaml.ansible: diff --git a/roles/mysql/handlers/main.yml b/roles/mysql/handlers/main.yml new file mode 100644 index 0000000..91ac8ea --- /dev/null +++ b/roles/mysql/handlers/main.yml @@ -0,0 +1,5 @@ +--- +- name: restart mysql + service: + name: "{{ mysql_service_name }}" + state: restarted diff --git a/roles/mysql/meta/main.yml b/roles/mysql/meta/main.yml new file mode 100644 index 0000000..5d50bf4 --- /dev/null +++ b/roles/mysql/meta/main.yml @@ -0,0 +1,60 @@ +galaxy_info: + author: your name + description: your description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Some suggested licenses: + # - BSD (default) + # - MIT + # - GPLv2 + # - GPLv3 + # - Apache + # - CC-BY + license: license (GPLv2, CC-BY, etc) + + min_ansible_version: 2.4 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # Optionally specify the branch Galaxy will use when accessing the GitHub + # repo for this role. During role install, if no tags are available, + # Galaxy will use this branch. During import Galaxy will access files on + # this branch. If Travis integration is configured, only notifications for this + # branch will be accepted. Otherwise, in all cases, the repo's default branch + # (usually master) will be used. + #github_branch: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. \ No newline at end of file diff --git a/roles/mysql/tasks/main.yml b/roles/mysql/tasks/main.yml new file mode 100644 index 0000000..aa0fe87 --- /dev/null +++ b/roles/mysql/tasks/main.yml @@ -0,0 +1,39 @@ +--- +- name: gather OS distribution version specific variables + include_vars: "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yaml" + +- name: gather OS distribution specific variables + include_vars: "{{ ansible_distribution }}.yaml" + +- name: gather OS family specific variables + include_vars: "{{ ansible_os_family }}.yaml" + +- name: manage mysql package + package: + name: "{{ mysql_package_name }}" + state: "{{ mysql_package_state }}" + +- name: create datadir + file: + path: "{{ mysql_config.mysqld.datadir }}" + owner: "{{ mysql_datadir_owner }}" + group: "{{ mysql_datadir_group }}" + mode: "{{ mysql_datadir_mode }}" + state: directory + +- name: initialize mysql + command: "mysqld --initialize --log-error={{ mysql_initialize_log_error }}" + args: + creates: "{{ mysql_config.mysqld.datadir }}/mysql" + +- name: configure mysql + template: + src: my.cnf.j2 + dest: "{{ mysql_cfg_path }}" + notify: restart mysql + +- name: manage mysql service + service: + name: "{{ mysql_service_name }}" + state: "{{ mysql_service_state }}" + enabled: "{{ mysql_service_enabled }}" diff --git a/roles/mysql/templates/my.cnf.j2 b/roles/mysql/templates/my.cnf.j2 new file mode 100644 index 0000000..07f21e4 --- /dev/null +++ b/roles/mysql/templates/my.cnf.j2 @@ -0,0 +1,12 @@ +# {{ ansible_managed }} +{% for section, cfg in mysql_config.iteritems() | sort %} + +[{{section}}] +{% for k, v in cfg.iteritems() | sort %} +{% if k is defined and v is not none %} +{{ k }} = {{ v }} +{% elif k and v is none %} +{{ k }} +{% endif %} +{% endfor %} +{% endfor %} diff --git a/roles/mysql/tests/inventory b/roles/mysql/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/roles/mysql/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/roles/mysql/tests/test.yml b/roles/mysql/tests/test.yml new file mode 100644 index 0000000..56f6b9c --- /dev/null +++ b/roles/mysql/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - roles/mysql \ No newline at end of file diff --git a/roles/mysql/vars/Debian.yaml b/roles/mysql/vars/Debian.yaml new file mode 100644 index 0000000..54996e2 --- /dev/null +++ b/roles/mysql/vars/Debian.yaml @@ -0,0 +1,2 @@ +--- +mysql_cfg_path: /etc/my.cnf diff --git a/roles/mysql/vars/Ubuntu-18.yaml b/roles/mysql/vars/Ubuntu-18.yaml new file mode 100644 index 0000000..d7f7b0b --- /dev/null +++ b/roles/mysql/vars/Ubuntu-18.yaml @@ -0,0 +1,5 @@ +--- +mysql_service_name: 'mysql.service' +mysql_cfg_path: /etc/mysql/my.cnf + +# vim:ft=yaml.ansible: diff --git a/roles/mysql/vars/Ubuntu.yaml b/roles/mysql/vars/Ubuntu.yaml new file mode 100644 index 0000000..86db525 --- /dev/null +++ b/roles/mysql/vars/Ubuntu.yaml @@ -0,0 +1,4 @@ +--- +mysql_package_name: 'mysql-server' + +# vim:ft=yaml.ansible: diff --git a/roles/mysql/vars/main.yml b/roles/mysql/vars/main.yml new file mode 100644 index 0000000..d8f8f40 --- /dev/null +++ b/roles/mysql/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for roles/mysql \ No newline at end of file