---
- hosts: all
  become: true
  connection: local
  vars:
      ansible_python_interpreter: /usr/local/bin/python2.7
  tasks:
    - name: install packages
      pkgng: name={{item}} state=present
      with_items:
        - vim-lite
        - bash
        - bash
        - bugzilla50
        - apache24
        - postgresql93-server
        - git
        - py27-psycopg2
        - p5-DBD-Pg

    - name: enable and start postgres
      service: name=postgresql enabled=yes
   
    - name: initialize postgres
      command: /usr/local/etc/rc.d/postgresql initdb
      args:
        creates: /usr/local/pgsql/data

    - name: enable and start postgres
      service: name=postgresql state=started enabled=yes

    - name: Create bugs database
      postgresql_db: db=bugs login_user=pgsql

    - name: Create postgresql bugs user
      postgresql_user: user=bugs db=bugs login_user=pgsql

    - name: run checksetup.pl
      command: /usr/local/www/bugzilla/checksetup.pl
      args:
        creates: /usr/local/www/bugzilla/localconfig

    - name: configure bugzilla
      lineinfile:
        dest: /usr/local/www/bugzilla/localconfig
        regexp: 'db_driver.*='
        line: "$db_driver = 'Pg';"

    - name: checkout FreeBSD bugzilla
      git:
        repo: https://github.com/freebsd/bugzilla.git
        dest: /usr/local/www/bugzilla-freebsd
        version: freebsd-local

    - synchronize:
        src: /usr/local/www/bugzilla-freebsd/
        dest: /usr/local/www/bugzilla/

    - name: re-run checksetup.pl
      command: /usr/local/www/bugzilla/checksetup.pl {{ playbook_dir }}/answer.pm
      args:
        creates: /usr/local/www/bugzilla/data/params.json

    - name: Make my directory tree readable
      file:
        path: /usr/local/www/bugzilla/
        owner: www
        group: www
        recurse: yes

    - name: enable and start apache24
      service: name=apache24 state=restarted
