db_mysql.yml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ---
  2. - name: "[mySQL] - Service is installed."
  3. ansible.builtin.package:
  4. name: "{{ 'default-' if ((ansible_distribution|lower) == 'debian' and nextcloud_db_backend == 'mysql') else '' }}{{ nextcloud_db_backend }}-server"
  5. state: present
  6. register: nc_mysql_db_install
  7. - name: "[mySQL] - Check if MySQL packages were installed."
  8. ansible.builtin.set_fact:
  9. mysql_install_packages: "{{ nc_mysql_db_install is defined and nc_mysql_db_install.changed }}"
  10. - name: "[mySQL] - Get MySQL version."
  11. ansible.builtin.command: 'mysql --version'
  12. register: mysql_cli_version
  13. changed_when: false
  14. check_mode: false
  15. - name: "[mySQL] - Packages are installed."
  16. ansible.builtin.package:
  17. name: "{{ nc_mysql_deps }}"
  18. state: present
  19. vars:
  20. nc_mysql_deps:
  21. - "php{{ php_ver }}-mysql"
  22. - "python3-pymysql"
  23. - name: "[mySQL] - Ensure MySQL is started and enabled on boot."
  24. ansible.builtin.service:
  25. name: "{{ mysql_daemon }}"
  26. state: started
  27. enabled: "{{ nextcloud_db_enabled_on_startup }}"
  28. register: mysql_service_configuration
  29. - name: "[mySQL] - Get list of hosts for the root user."
  30. ansible.builtin.command: mysql -NBe
  31. "SELECT Host
  32. FROM mysql.user
  33. WHERE User = 'root'
  34. ORDER BY (Host='localhost') ASC"
  35. register: mysql_root_hosts
  36. changed_when: false
  37. check_mode: false
  38. when: mysql_install_packages | bool or nextcloud_mysql_root_pwd_update
  39. # Note: We do not use mysql_user for this operation, as it doesn't always update
  40. # the root password correctly. See: https://goo.gl/MSOejW
  41. - name: "[mySQL] - Update MySQL root password for localhost root account (5.7.x)."
  42. ansible.builtin.shell: >
  43. mysql -u root -NBe
  44. 'ALTER USER "root"@"{{ item }}"
  45. IDENTIFIED WITH mysql_native_password BY "{{ nextcloud_mysql_root_pwd }}"; FLUSH PRIVILEGES;'
  46. with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
  47. when: >
  48. ((mysql_install_packages | bool) or nextcloud_mysql_root_pwd_update)
  49. and ('5.7.' in mysql_cli_version.stdout or '8.0.' in mysql_cli_version.stdout)
  50. - name: "[mySQL] - Update MySQL root password for localhost root account (< 5.7.x)."
  51. ansible.builtin.shell: >
  52. mysql -NBe
  53. 'SET PASSWORD FOR "root"@"{{ item }}" = PASSWORD("{{ nextcloud_mysql_root_pwd }}"); FLUSH PRIVILEGES;'
  54. with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
  55. when: >
  56. ((mysql_install_packages | bool) or nextcloud_mysql_root_pwd_update)
  57. and ('5.7.' not in mysql_cli_version.stdout and '8.0.' not in mysql_cli_version.stdout)
  58. - name: "[mySQL] - Copy .my.cnf file with root password credentials."
  59. ansible.builtin.template:
  60. src: "root-my.cnf.j2"
  61. dest: "/root/.my.cnf"
  62. owner: root
  63. group: root
  64. mode: 0600
  65. when: mysql_install_packages | bool or nextcloud_mysql_root_pwd_update
  66. - name: "[mySQL] - Get list of hosts for the anonymous user."
  67. ansible.builtin.command: mysql -NBe 'SELECT Host FROM mysql.user WHERE User = ""'
  68. register: mysql_anonymous_hosts
  69. changed_when: false
  70. check_mode: false
  71. - name: "[mySQL] - Remove anonymous MySQL users."
  72. mysql_user:
  73. name: ""
  74. host: "{{ item }}"
  75. state: absent
  76. with_items: "{{ mysql_anonymous_hosts.stdout_lines|default([]) }}"
  77. - name: "[mySQL] - Remove MySQL test database."
  78. mysql_db:
  79. name: 'test'
  80. state: absent
  81. - name: "[mySQL] - Set mysql config option for Nextcloud"
  82. ansible.builtin.copy:
  83. dest: /etc/mysql/conf.d/nextcloud.cnf
  84. src: files/mysql_nextcloud.cnf
  85. mode: 0600
  86. notify: restart mysql
  87. - name: "[mySQL] - Add Database {{ nextcloud_db_name }}."
  88. mysql_db:
  89. name: "{{ nextcloud_db_name }}"
  90. login_user: root
  91. login_password: "{{ nextcloud_mysql_root_pwd }}"
  92. config_file: "{{ mysql_credential_file[(ansible_os_family|lower)] | default(omit) }}"
  93. state: present
  94. - name: "[mySQL] - Configure the database user."
  95. mysql_user:
  96. name: "{{ nextcloud_db_admin }}"
  97. password: "{{ nextcloud_db_pwd }}"
  98. priv: "{{ nextcloud_db_name }}.*:ALL"
  99. login_user: root
  100. login_password: "{{ nextcloud_mysql_root_pwd }}"
  101. config_file: "{{ mysql_credential_file[(ansible_os_family|lower)] | default(omit) }}"
  102. state: present