- name: ensure sites directory is present
  file: path={{web_dir}} state=directory

- name: fetch wordpress source 
  command: chdir={{ web_dir }} wget http://wordpress.org/latest.tar.gz 

- name: create current folder
  file: path={{web_dir}}/{{app_name}}/current/ state=directory owner=deploy

- name: create blog folder
  file: path={{web_dir}}/{{app_name}}/current/blog/ state=directory owner=deploy

- name: create conf folder
  file: path={{web_dir}}/{{app_name}}/current/conf/ state=directory owner=deploy

- name: expand wordpress source 
  sudo: yes
  command:  chdir={{web_dir}} tar xzvf latest.tar.gz -C {{web_dir}}/{{app_name}}/current/blog/ --strip 1

- name: remove wordpress source
  file: path={{web_dir}}/latest.tar.gz state=absent

- name: give www-data ownership of installed files
  sudo: yes
  file: path={{web_dir}}/{{app_name}}/current/blog/ state=directory owner=www-data group=deploy recurse=yes

- name: create {{mysql_dbname }}  database
  mysql_db: name={{mysql_dbname }}  state=present

- name: create wordpress user
  mysql_user: name={{mysql_user }} password={{ mysql_user_pass }} priv=*.*:ALL state=present

- name: create nginx site config
  template: src=nginx.conf dest={{web_dir}}/{{app_name}}/current/conf/nginx.conf

- name: link nginx site config
  sudo: yes
  file: src={{web_dir}}/{{app_name}}/current/conf/nginx.conf dest=/etc/nginx/sites-enabled/{{app_name}} state=link
  notify:
    - Restart nginx
