secure-installation.yml 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. - name: Remove mysql users.
  59. mysql_user:
  60. name: "{{ item }}"
  61. host: "localhost"
  62. state: absent
  63. with_items:
  64. - mysql
  65. - name: Remove MySQL test database.
  66. mysql_db: "name='test' state=absent"