Last active
May 9, 2023 01:23
-
-
Save zavan/177d7b8b4a5d35f0b13e3491323292bd to your computer and use it in GitHub Desktop.
Import system timezones to MySQL and configure its default timezone with Ansible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vars: | |
timezone: America/Sao_Paulo | |
- name: Get current MySQL timezone | |
become: yes | |
mysql_variables: | |
variable: time_zone | |
register: mysql_tz | |
- set_fact: | |
incorrect_mysql_tz: "{{ mysql_tz.msg != timezone }}" | |
- name: Change MySQL timezone | |
when: incorrect_mysql_tz | |
become: yes | |
block: | |
- name: Check if MySQL has our time zone in the time_zone_name table | |
command: "mysql -D mysql -Ns -e \"SELECT EXISTS (SELECT * FROM time_zone_name WHERE name = '{{ timezone }}' LIMIT 1)\"" | |
## \/ This is available on the devel version of Ansible \/ | |
# mysql_query: | |
# login_db: mysql | |
# query: SELECT EXISTS (SELECT * FROM time_zone_name WHERE name = 'US/Central' LIMIT 1) | |
register: query_result | |
- set_fact: | |
table_has_timezone: "{{ query_result.stdout | bool }}" | |
- name: Import system time zones into MySQL | |
when: not table_has_timezone | |
block: | |
- name: Generate sql file from system time zones | |
shell: 'mysql_tzinfo_to_sql /usr/share/zoneinfo > /tmp/mysql_tz.sql' | |
args: | |
creates: /tmp/mysql_tz.sql | |
- name: Import generated MySQL time zones file | |
mysql_db: | |
state: import | |
name: mysql | |
target: /tmp/mysql_tz.sql | |
- name: Add MySQL time zone to config | |
lineinfile: | |
path: /etc/mysql/my.cnf | |
insertafter: \[mysqld\] | |
firstmatch: yes | |
line: "default-time-zone='{{ timezone }}'" | |
register: add_tz_to_cnf | |
- name: Restart MySQL | |
when: add_tz_to_cnf.changed | |
debug: msg="Restart MySQL so it picks up the new timezone" | |
notify: restart mysql |
has comprado el Usdt de los 100$
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good