---

- include_vars: "{{ item }}"
  with_first_found:
    - ../includes/vars/{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml
    - ../includes/vars/{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml
    - ../includes/vars/{{ ansible_distribution }}.yml
    - ../includes/vars/{{ ansible_os_family }}.yml
    - ../includes/vars/defaults.yml

- name: Install MySQL tools
  package: name={{ webapps_mysql_packages }}
  when: mysql_packages_installed is not defined

- set_fact: mysql_packages_installed=True

- name: Create MySQL database
  mysql_db:
    name: "{{ db_name }}"
    login_host: "{{ db_server | default(mysql_server) }}"
    login_port: "{{ db_port | default(omit) }}"
    login_user: sqladmin
    login_password: "{{ mysql_admin_pass }}"
    state: present
    encoding: "{{ db_encoding | default('utf8mb4') }}"
    collation: "{{ db_collation | default('utf8mb4_general_ci') }}"
  register: db_created

- name: Create MySQL User
  mysql_user:
    name: "{{ db_user }}"
    password: "{{ db_pass }}"
    priv: "{{ db_name }}.*:ALL"
    append_privs: "{{ append_privs | default(False) }}"
    host: "{{ ( db_server == 'localhost' ) | ternary('localhost', item ) }}"
    login_host: "{{ db_server }}"
    login_port: "{{ db_port | default(omit) }}"
    login_user: sqladmin
    login_password: "{{ mysql_admin_pass }}"
    state: present
  with_items: "{{ ansible_all_ipv4_addresses }}"

