---
- name: install mariadb
  pacman: name=mariadb,python-mysqlclient state=present

- name: initialize mariadb
  command: mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
  args:
    creates: /var/lib/mysql/mysql

- name: configure mariadb
  template: src=server.cnf.j2 dest=/etc/my.cnf.d/server.cnf owner=root group=root mode=0644
  notify:
    - restart mariadb

- name: start and enable the service
  service: name=mariadb state=started enabled=yes

- name: delete anonymous users
  mysql_user: user='' host_all=yes state='absent'

- name: disallow remote root login
  command: 'mysql -NBe "{{ item }}"'
  with_items:
    - DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')
  changed_when: false

- name: drop test database
  mysql_db: db=test state=absent

- name: set root password
  mysql_user: user=root host={{ item }} password={{ vault_mariadb_users.root }}
  with_items:
    - '127.0.0.1'
    - '::1'
    - 'localhost'
  no_log: true

- name: create client configuration for root
  template: src=client.cnf.j2 dest=/root/.my.cnf owner=root group=root mode=0644
  no_log: true
