Changes the root password

This commit is contained in:
Sergio Rua 2023-12-22 12:51:51 +01:00
parent a4a5a91b4a
commit 0ca721bd17
No known key found for this signature in database
GPG Key ID: AD5C658B2DA15D37
4 changed files with 50 additions and 0 deletions

View File

@ -78,6 +78,11 @@ mariadb_mysqldump_raw: |
quote-names
max_allowed_packet = 16M
# Changes the root password the first time and uses this variable
# to connect to mariadb for any other operations
#mariadb_root_password: changeme
#mariadb_create_root_my_cnf: false
# Databases
mariadb_databases: []
# - name: db1

View File

@ -28,3 +28,7 @@
- name: Immediately restart MariaDB (necessary for replication)
ansible.builtin.meta: flush_handlers
- name: Root password
ansible.builtin.include: root-password.yml
when: mariadb_root_password is defined and mariadb_root_password != ""

39
tasks/root-password.yml Normal file
View File

@ -0,0 +1,39 @@
---
- name: Check if the specified root password is already set
ansible.builtin.shell: >
mysqladmin -u root status
changed_when: false
failed_when: false
no_log: true
become: true
ignore_errors: true
register: _mariadb_password_check
tags: mariadb
- name: Change the root password
community.mysql.mysql_user:
name: "root"
host: "localhost"
password: "{{ mariadb_root_password }}"
login_unix_socket: "{{ mariadb_unix_socket }}"
no_log: true
run_once: true
when: _mariadb_password_check.rc == 0
tags: root_password
- name: Create /root/.my.cnf
ansible.builtin.copy:
dest: "/root/.my.cnf"
mode: "0640"
owner: root
group: root
content: |
[client]
user=root
password={{ mariadb_root_password}}
become: true
when:
- mariadb_root_password is defined and mariadb_root_password != ""
- mariadb_create_root_my_cnf is defined and mariadb_create_root_my_cnf
# code: language=ansible

View File

@ -9,5 +9,7 @@
append_privs: "{{ item.append_privs | default('no') }}"
encrypted: "{{ item.encrypted | default('no') }}"
login_unix_socket: "{{ mariadb_unix_socket }}"
login_user: root
login_password: "{{ mariadb_root_password | default(omit) }}"
loop: "{{ mariadb_users }}"
no_log: true