- hosts: servers
  become: yes
  tasks:
  - name: Do apt-get update
    apt:
      update_cache: yes
      cache_valid_time: 3600

  - name: Install nginx
    apt:
      name: ['nginx']
      state: latest

  - name: Create www directory
    file:
      path: /var/www/{{ domain }}
      state: directory
      mode: '0775'
      owner: "{{ ansible_user }}"
      group: "{{ ansible_user }}"

  - name: Delete default nginx site
    file:
      path: /etc/nginx/sites-enabled/default
      state: absent
    notify: restart nginx

  - name: copy nginx site.conf
    template:
      src: site.conf.j2
      dest: /etc/nginx/sites-enabled/{{ domain }}
      owner: root
      group: root
      mode: '0644'
    notify: restart nginx

  handlers:
    - name: restart nginx
      service:
        name: nginx
        state: restarted