---
- hosts: vagrant
  remote_user: vagrant
  sudo: yes
  tasks:
    - name: Install unzip
      apt: pkg=unzip state=latest update_cache=yes

    - name: Install MySQL client, server and related libraries
      apt: pkg={{ item }} state=latest
      with_items:
        - mysql-client
        - mysql-server
        - python-mysqldb

    - name: Install PHP and its modules
      apt: pkg={{ item }} state=latest
      with_items:
        - php5
        - php5-cli
        - php5-curl
        - php5-gd
        - php5-imagick
        - php5-mysql
        - php5-xmlrpc
        - php5-xdebug

    - name: Install custom php error config
      template:
        src: "{{ phpErrorTemplatePath }}"
        dest: "/etc/php5/conf.d/php_error.ini"
        owner: "root"
        mode: 0600

    - name: touch php error log file
      copy:
        content: ""
        dest: /var/log/apache2/php_err.log
        force: no
        group: www-data
        owner: www-data
        mode: 0755

    - name: Install Apache and its modules
      apt: pkg={{ item }} state=latest
      with_items:
        - apache2
        - libapache2-mod-php5

    - name: Activate mod_rewrite
      apache2_module: 
        name: rewrite
        state: present

    - name: Start MySQL service
      service:
        name: "mysql"
        state: started
        enabled: yes

    - name: Setup MySQL root password
      mysql_user:
        name: "root"
        password: "mysql"
        host: "{{ item }}"
        state: present
      with_items:
        - "{{ ansible_hostname }}"
        - 127.0.0.1
        - ::1
        - localhost

    - name: Setup MySQL creds for root user
      template:
        src: "{{ mysqlTemplatePath }}"
        dest: "/root/.my.cnf"
        owner: "root"
        mode: 0600

    - name: Delete blank MySQL users
      mysql_user:
        name: ""
        host: "{{ item }}"
        state: absent
      with_items:
        - "{{ ansible_hostname }}"
        - 127.0.0.1
        - ::1
        - localhost

    - name: Drop MySQL test database
      mysql_db: 
        name: test
        state: absent

    - name: Setup empty database for WordPress
      mysql_db:
        name: "wordpress"
        encoding: "utf8"
        collation: "utf8_unicode_ci"
        state: "present"
        login_user: "root"
        login_password: "mysql"

    - name: Setup MySQL user for WordPress
      mysql_user:
        name: "user_wp"
        password: "wordpress"
        host: "localhost"
        priv: "wordpress.*:ALL"
        state: "present"

    - name: Put "vagrant" user in www-data group
      user:
        name: "vagrant"
        groups: "www-data"
        append: yes

    - name: Set up XDebug for remote debugging
      template:
        src: "{{ xdebugTemplatePath }}"
        dest: /etc/php5/apache2/conf.d/xdebug-remote-debugging.ini       

    - name: make directory for wordpress
      file: path=/var/www/wordpress state=directory owner=www-data group=www-data mode=0755

    - name: download WordPress
      get_url: url=http://wordpress.org/latest.zip dest=/tmp/

    - name: unzip WordPress
      command: unzip /tmp/latest.zip -d /tmp creates=/var/www/wordpress/readme.html

    - name: move WordPress files to web root
      shell: mv /tmp/wordpress/* /var/www/wordpress/ creates=/var/www/wordpress/readme.html

    - name: set WordPress file permissions correctly
      file: path=/var/www/wordpress state=directory recurse=yes owner=www-data group=www-data mode=0775

    - name: create upload dir for wordpress
      file: path=/var/www/wordpress/wp-content/uploads state=directory owner=www-data group=www-data mode=0755

    - name: Copy virtual host setup over
      template: 
        src: "{{ vhostTemplatePath }}"
        dest: /etc/apache2/sites-available/

    - name: Activate virtual host
      command: a2ensite vhost.conf

    - name: Deactivate default vhost
      command: a2dissite 000-default

    - name: Ensure Apache is running
      service: 
        name: apache2 
        state: restarted 
        enabled: yes

    - name: download wp-cli
      sudo: yes
      get_url: url=https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar dest=/usr/local/bin/wp

    - name: update permissions of wp-cli to allow anyone to execute it (should be only for local install!)
      file: path=/usr/local/bin/wp mode="0755"

    - name: WP config
      command: wp core config --allow-root --dbname=wordpress --dbuser=root --dbpass=mysql --force
      args:
        chdir: /var/www/wordpress
 
    - name: do WP install
      command: wp core install --allow-root --url='192.168.50.50' --title='wp plugindev' --admin_user=admin --admin_password=admin --admin_email=blah@blah.com --skip-email
      args:
        chdir: /var/www/wordpress

    - name: set Permalinks structure
      command: wp --allow-root rewrite structure '/%year%/%monthnum%/%postname%'
      args:
        chdir: /var/www/wordpress

    - name: copy plugin files to WP install
      command: cp -r /plugin /var/www/wordpress/wp-content/plugins/wordpress-static-html-output
 
    - name: plugin install
      command: wp --allow-root plugin activate wordpress-static-html-output
      args:
        chdir: /var/www/wordpress
 
