---
 - name: Install openresty
   become: yes
   package:
     name: "{{ item }}"
     state: present
   loop:
     - openresty
     - php-fpm

 - name: Copy PHP config
   become: yes
   copy:
     src: php.ini
     dest: /etc/php/php.ini
     owner: root
     group: root
     mode: 0755

 - name: Copy Webserver config
   become: yes
   copy:
     src: webserver.service
     dest: /usr/lib/systemd/system/webserver.service
     owner: http
     group: http
     mode: 0660
   register: servicefile

 - systemd:
     daemon_reload: true
   when: servicefile.changed
   become: yes

 - name: Copy conf.d
   become: yes
   copy:
     src: "conf.d/{{ inventory_hostname }}"
     dest: /opt/openresty/nginx/conf.d
     owner: http
     group: http
     mode: 0660
     directory_mode: 0770
   register: confd

 - name: Copy conf
   become: yes
   copy:
     src: conf/
     dest: /opt/openresty/nginx/conf/
     owner: http
     group: http
     mode: 0660
   register: conf

 - name: Ensure default openresty service file is off.
   become: yes
   service:
     name: openresty
     state: stopped
     enabled: no 

 - name: Ensure service is started
   become: yes
   when: conf.changed or confd.changed
   service:
     name: "{{ item }}" 
     enabled: yes
     state: restarted
   loop:
     - php-fpm
     - webserver
