---
# - name : Include Systembase Yum Install Roles
#   include_role: 
#     name: systembase
#     tasks_from: packages
#   tags: nginx_installer

- name : Include Systembase Sysctl Configurtion Roles
  include_role:
    name: systembase
    tasks_from: sysctl
  tags: nginx_installer
  

- name: Install Library On Ubuntu
  apt:  "name={{ item }} state=latest"
  with_items:
    - unzip
    - build-essential
    - libreadline-dev
    - libncurses5-dev
    - libpcre3-dev
    - libssl-dev
    - git 
    - perl 
    - make
    - libjemalloc-dev 
    - daemon
  when: ansible_distribution == 'Ubuntu'
  tags: nginx_installer

- name: Install Library On CentOS
  yum:  "name={{ item }} state=latest"
  with_items:
    - unzip
    - readline-devel
    - pcre-devel
    - openssl-devel
    - gcc
  when: ansible_distribution == 'CentOS'
  tags: nginx_installer

- name: Get PCPE Version Of RPM
  shell: yum list installed pcre-devel | grep custom-rpm | awk '{print $2}' | cut -d'-' -f1
  register: pcre_version
  changed_when: False
  args:
    warn: no
  tags: nginx_installer

- name: Copy PCRE Library On CentOS6
  copy: src=pcre-8.41.zip dest=/opt/pcre-8.41.zip owner=root group=root mode=0444
  when: pcre_version.stdout_lines   < '8.4' and ansible_distribution == 'CentOS'
  tags: nginx_installer

- name: Unzip PCRE Library
  unarchive:
    src: /opt/pcre-8.41.zip
    dest: /opt/
    remote_src: yes
  when: pcre_version.stdout_lines   < '8.4' and ansible_distribution == 'CentOS'
  tags: nginx_installer

#- name: Define Variable For PCRE
#vars:
#    pcre_path: "/opt/pcre-8.41"
#  when: pcre_version.stdout_lines   < '8.4' and ansible_distribution == 'CentOS'
#  tags: nginx_installer
 
- group:
    name: nginx
    state: present
  tags: nginx_installer

- user: 
    name: nginx
    shell: /sbin/nologin
    group: nginx 
  tags: nginx_installer

- name: Copy Source Package
  copy: src=openresty-{{ openresty_version }}.tar.gz dest=/root/openresty-{{ openresty_version }}.tar.gz owner=root group=root mode=0644
  tags: nginx_installer

- name: Copy Install Stript When Pcre_version  Less Than 8.4
  template: src=openresty.sh dest=/root/openresty.sh owner=root group=root mode=0444
  vars:
    pcre_path: "/opt/pcre-8.41"
  tags: nginx_installer
  when: pcre_version.stdout_lines   < '8.4' and ansible_distribution == 'CentOS'

- name: Copy Install Stript
  template: src=openresty.sh dest=/root/openresty.sh owner=root group=root mode=0444
  tags: nginx_installer
  when: pcre_version.stdout_lines   > '8.4' or ansible_distribution == 'Ubuntu'

- name: Copy Service
  copy: src=nginxd dest=/etc/init.d/nginxd owner=root group=root mode=0744
  tags: nginx_installer

- name: Run Installer
  shell: bash /root/openresty.sh
  tags: nginx_installer

- name: Create Cache Directory
  file:
    path: "{{ item }}"
    state: directory
    mode: 0755
  with_items:
    - "/usr/local/openresty/nginx/cache/proxy_cache"
    - "/usr/local/openresty/nginx/cache/temp_path"
    - "/usr/local/openresty/nginx/cache/fastcgi_cache"
    - "/usr/local/openresty/nginx/conf/vhost"
    - "/usr/local/openresty/nginx/run"
  tags: nginx_installer

- name: Copy nginx.conf
  template: src=nginx.conf dest=/usr/local/openresty/nginx/conf/nginx.conf  owner=root group=root mode=0644
  tags: nginx_installer

- name: Create Empty Deny File
  file:
    path: /usr/local/openresty/nginx/conf/deny.ip
    state: touch
    mode: 0644
  tags: nginx_installer

#- name: Create Empty Run Path
#  file:
#    path: /usr/local/openresty/nginx/run
#    state: directory
#    mode: 0755

- name: Remove Pid File & Temp Files
  file:
    path: "{{ item }}"
    state: absent
  with_items:
    - "/run/nginx.pid"
    - "/root/openresty-1.11.2.4"
    - "/root/openresty-1.11.2.4.tar.gz"
    - "/root/openresty.sh"
    - "/opt/pcre-8.41"
  tags: nginx_installer
 
- name: Nginx Service Enable
  service:
    name: nginxd
    enabled: yes
#- name: Starting Nginx Process
#  shell: /etc/init.d/nginxd start
