secure-installation.yml 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 }}"'
  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
  26. "SELECT Host
  27. FROM mysql.user
  28. WHERE User = '{{ mysql_root_username }}'
  29. ORDER BY (Host='localhost') ASC"
  30. register: mysql_root_hosts
  31. changed_when: false
  32. check_mode: false
  33. # Set root password for MySQL
  34. - name: Update MySQL root password for localhost root account
  35. shell: >
  36. mysql -NBe
  37. 'SET PASSWORD FOR "{{ mysql_root_username }}"@"{{ item }}" = PASSWORD("{{ mysql_root_password }}"); FLUSH PRIVILEGES;'
  38. with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
  39. # Has to be after the root password assignment, for idempotency.
  40. - name: Copy .my.cnf file with root password credentials.
  41. template:
  42. src: "root-my.cnf.j2"
  43. dest: "/root/.my.cnf"
  44. owner: root
  45. group: root
  46. mode: 0600
  47. - name: Get list of hosts for the anonymous user.
  48. command: mysql -NBe "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. no_log: true
  59. - name: Remove MySQL test database.
  60. mysql_db: "name='test' state=absent"