---
# Configure EC2 NetBSD instance as database server
#
# Assumes:
#  - Basic EC2 NetBSD config done
#
# Run: 
#    env ANSIBLE_HOSTS=./ec2.py ansible-playbook config-ec2-dbserver.yml
#
# Copyright (c) 2013 Hubert Feyrer <hubert@feyrer.de>
#

 - hosts:
   - security_group_ec2-webservers
   user: feyrer
   sudo: yes
   gather_facts: false

   tasks:
    - name: Install mysql
      action: pkgin name=mysql-server-5.1.65 state=present

    - name: Install MySQL rc.d script
      template: src=/usr/pkg/share/examples/rc.d/mysqld dest=/etc/rc.d/mysqld mode=0755

    - name: Start MySQL service
      service: name=mysqld enabled=yes state=started

    - name: Install python-mysqldb (for mysql_user module)
      action: pkgin name=py27-mysqldb state=present 

    - name: Setup DB 
      mysql_db: db=webapp state=present

    - name: Add db-user
      mysql_user: name=webapp password=webapp state=present priv='webapp.*:INSERT,UPDATE,DROP,CREATE,ALTER,LOCK TABLES,SELECT'

    - name: Copy over DB template
      copy: src=db/dump-names.sql dest=/tmp/dump-names.sql

    - name: Import DB data
      mysql_db: db=webapp state=import target=/tmp/dump-names.sql  login_user=webapp login_password=webapp 

#   - name: Allow remote-login of db-user

    - name: Clean up
      command: rm /tmp/dump-names.sql
