---
# On some installations MySQL generates a temporary root password during startup.
# These tasks change that temporary password to the configured mysql_root_password early.
# We need to adjust .my.cnf here so that the other tasks will work as expected.

- name: get temporary root password from log file
  shell: "grep -s -o 'temporary password.*' /var/log/mysqld.log | cut -d: -f2 | sed 's/[[:space:]]//g'"
  register: mysql_temporary_root_password
  changed_when: false

- name: reset expired temporary root password
  command: "mysql -uroot -p{{ mysql_temporary_root_password.stdout }} --connect-expired-password -NBe \"ALTER USER root@localhost IDENTIFIED BY '{{ mysql_root_password }}'\""
  when: "mysql_temporary_root_password.stdout != ''"

- name: ensure temporary root password is not present in log file
  lineinfile:
    dest: /var/log/mysqld.log
    regexp: "temporary password"
    state: absent
  when: "mysql_temporary_root_password.stdout != ''"

- include: my_cnf.yml
  when: "mysql_temporary_root_password.stdout != ''"

---
# On some installations MySQL generates a temporary root password during startup.
# These tasks change that temporary password to the configured mysql_root_password early.
# We need to adjust .my.cnf here so that the other tasks will work as expected.

- name: get temporary root password from log file
  shell: "grep -s -o 'temporary password.*' /var/log/mysqld.log | sed 's/.*: \(.\+\)/\1/'"
  register: mysql_temporary_root_password
  changed_when: false

- name: reset expired temporary root password
  command: "mysql -uroot -p{{ mysql_temporary_root_password.stdout }} --connect-expired-password -NBe \"ALTER USER root@localhost IDENTIFIED BY '{{ mysql_root_password }}'\""
  when: "mysql_temporary_root_password.stdout != ''"

- name: ensure temporary root password is not present in log file
  lineinfile:
    dest: /var/log/mysqld.log
    regexp: "temporary password"
    state: absent
  when: "mysql_temporary_root_password.stdout != ''"

- include: my_cnf.yml
  when: "mysql_temporary_root_password.stdout != ''"

Support auto generated root passwords which contain ':'
Support auto generated root passwords which contain ':'
