---
# Configure NetBSD machine as PHP web application server & deploy web-app
#
# XXX BEWARE! This playbok uses some private extensions to the 'lintinfile'
# module that I tried to provide upstreams but which were eventually
# solved diferently upstream. As such, expect problems to run this 1:1.
# Drop me a note it you need assistance or have questions!
#
# Assumes:
#  - Basic NetBSD config done
#  - Database setup done
#
# Run:
#    ansible-playbook -i hosts-HF config-netbsd-webserver.yml
#    ansible-playbook -i hosts-HF config-netbsd-webserver.yml --tags=deploy
#
# Results:
#    http://10.0.0.181/phptest.php
#    http://10.0.0.181/phpmyadmin/
#    http://10.0.0.181/webapp/
#
# Copyright (c) 2013 Hubert Feyrer <hubert@feyrer.de>
#

 - hosts: webservers
   user: feyrer
   sudo: yes
   vars:
       htdocs: /usr/pkg/share/httpd/htdocs

   tasks:
    - name: Installing ap24-php53 package and dependencies
      action: pkgin name=ap24-php53 state=present

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

    - name: Enable and start Apache service
      service: name=apache enabled=yes state=started

    - name: Enable PHP in Apache config file
      lineinfile: "dest=/usr/pkg/etc/httpd/httpd.conf regexp='${item.re}' line='${item.l}'"
      with_items:
       - { re: 'LoadModule.*mod_php5.so', l: 'LoadModule php5_module lib/httpd/mod_php5.so' }
       - { re: 'AddHandler.*x-httpd-php', l: 'AddHandler application/x-httpd-php .php' }
      notify:
       - restart apache

    - name: Make Apache read index.php
      lineinfile: dest='/usr/pkg/etc/httpd/httpd.conf' line=' index.php'
                  append_at='DirectoryIndex' regexp='DirectoryIndex.*index.php'
                  state='present'
      notify:
       - restart apache

    - name: Add simple PHP test - see http://10.0.0.181/phptest.php
      copy: src=webapp/phptest.php dest=${htdocs}/phptest.php

    - name: Install phpmyadmin
      action: pkgin name=phpmyadmin state=present

    - name: Enable phpmyadmin in Apache config
      lineinfile: dest='/usr/pkg/etc/httpd/httpd.conf' regexp='^Include .*phpmyadmin/apache.conf' line='Include /usr/pkg/share/examples/phpmyadmin/apache.conf'
      notify:
       - restart apache

    - name: Frob Apache access control for phpmyadmin
      lineinfile: dest='/usr/pkg/share/examples/phpmyadmin/apache.conf' line='Require all granted' insertbefore='Options Indexes' regexp='Require all granted' state='present'
      notify:
       - restart apache

    - name: Enable PHP modules in PHP config file
      lineinfile: dest=/usr/pkg/etc/php.ini regexp='${item.re}' line='${item.l}'
      with_items:
       - { re: '^extension.*zlib.so',     l: 'extension=zlib.so' }
       - { re: '^extension.*zip.so',      l: 'extension=zip.so' }
       - { re: '^extension.*mysqli.so',   l: 'extension=mysqli.so' }
       - { re: '^extension.*mysql.so',    l: 'extension=mysql.so' }
       - { re: '^extension.*mcrypt.so',   l: 'extension=mcrypt.so' }
       - { re: '^extension.*mbstring.so', l: 'extension=mbstring.so' }
       - { re: '^extension.*json.so',     l: 'extension=json.so' }
       - { re: '^extension.*gd.so',       l: 'extension=gd.so' }
       - { re: '^extension.*gettext.so',  l: 'extension=gettext.so' }
       - { re: '^extension.*bz2.so',      l: 'extension=bz2.so' }
      notify:
       - restart apache

    - name: Create directory for webapp
      file: state=directory path='${htdocs}/webapp'

    - name: Deploy example webapp
      copy: src=webapp/webapp.php dest=${htdocs}/webapp/webapp.php
      tags:
       - deploy

    - name: Create webapp symlink for easy access
      file: src='${htdocs}/webapp/webapp.php' path='${htdocs}/webapp/index.php' state=link

   handlers:
    - name: restart apache
      action: service name=apache state=restarted
