secure-installation.yml 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ---
  2. #- name: Ensure default user is present.
  3. # mysql_user:
  4. # name: "{{ mysql_user_name }}"
  5. # host: 'localhost'
  6. # password: "{{ mysql_user_password }}"
  7. # priv: '*.*:ALL,GRANT'
  8. # state: present
  9. # when: mysql_user_name != mysql_root_username
  10. #
  11. ## Has to be after the password assignment, for idempotency.
  12. #- name: Copy user-my.cnf file with password credentials.
  13. # template:
  14. # src: "user-my.cnf.j2"
  15. # dest: "/root/.my.cnf"
  16. # owner: "{{ mysql_user_name }}"
  17. # mode: 0600
  18. # when: mysql_user_name != mysql_root_username
  19. - name: Disallow root login remotely
  20. command: 'mysql -NBe "{{ item }}" -S {{ mariadb_socket }}'
  21. with_items:
  22. - DELETE FROM mysql.user WHERE User='{{ mysql_root_username }}' AND Host NOT IN ('localhost', '127.0.0.1', '::1')
  23. changed_when: false
  24. - name: Get list of hosts for the root user.
  25. command: 'mysql -NBe "{{ item }}" -S {{ mariadb_socket }}'
  26. with_items:
  27. - SELECT Host FROM mysql.user WHERE User='{{ mysql_root_username }}' ORDER BY (Host='localhost') ASC
  28. register: mysql_root_hosts
  29. changed_when: false
  30. check_mode: false
  31. # Set root password for MySQL
  32. - name: Update MySQL root password for localhost root account
  33. shell: >
  34. mysql -NBe
  35. 'SET PASSWORD FOR "{{ mysql_root_username }}"@"{{ item }}" = PASSWORD("{{ mysql_root_password }}"); FLUSH PRIVILEGES;'
  36. with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
  37. # Has to be after the root password assignment, for idempotency.
  38. - name: Copy .my.cnf file with root password credentials.
  39. template:
  40. src: "root-my.cnf.j2"
  41. dest: "/root/.my.cnf"
  42. owner: root
  43. group: root
  44. mode: 0600
  45. - name: Get list of hosts for the anonymous user.
  46. command: 'mysql -NBe "{{ item }}" -S {{ mariadb_socket }}'
  47. with_items:
  48. - SELECT Host FROM mysql.user WHERE User = ''
  49. register: mysql_anonymous_hosts
  50. changed_when: false
  51. check_mode: false
  52. - name: Remove anonymous MySQL users.
  53. mysql_user:
  54. name: ""
  55. host: "{{ item }}"
  56. state: absent
  57. with_items: "{{ mysql_anonymous_hosts.stdout_lines|default([]) }}"
  58. - name: Remove mysql users.
  59. mysql_user:
  60. name: "{{ item }}"
  61. login_unix_socket: "{{ mariadb_socket }}"
  62. state: absent
  63. with_items:
  64. - mysql
  65. - name: Remove MySQL test database.
  66. mysql_db:
  67. name: test
  68. state: absent
  69. login_unix_socket: "{{ mariadb_socket }}"