#####################################
### someone's ansible provisioner ###
#####################################
# Part of: https://git.somenet.org/root/pub/somesible.git
# 2017-2025 by someone <someone@somenet.org>
#

- name: install web-server
  apt:
    pkg:
    - nginx-full
    - libwww-perl
    state: present
    policy_rc_d: 101
  tags: "online"
  ignore_errors: "{{ignore_online_errors | bool}}"


- name: copy nginx.conf
  copy:
    src: "{{item}}"
    dest: "/etc/nginx/nginx.conf"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/nginx.conf"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/nginx.conf"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/nginx.conf"
    - "default/nginx.conf"
  notify: restart nginx.service


- name: copy default vhost
  copy:
    src: "{{item}}"
    dest: "/etc/nginx/sites-enabled/000-default.vhost"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/000-default.vhost"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/000-default.vhost"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/000-default.vhost"
    - "default/000-default.vhost"
  register: temp_result


- name: enable and restart nginx.service when default vhost changes
  systemd:
    name: nginx.service
    daemon_reload: yes
    enabled: yes
    state: restarted
  when: temp_result.changed


- name: copy default robots.txt
  copy:
    src: "{{item}}"
    dest: "/var/www/html/robots.txt"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/robots.txt"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/robots.txt"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/robots.txt"
    - "default/robots.txt"
  notify: restart nginx.service


- name: copy default maintenance.html
  copy:
    src: "{{item}}"
    dest: "/var/www/maintenance.html.dis"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/maintenance.html"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/maintenance.html"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/maintenance.html"
    - "default/maintenance.html"
  notify: restart nginx.service


- name: fix permissions of /var/www/
  file:
    path: "/var/www/"
    state: directory
    mode: 0755
    owner: "root"
    group: "root"


- name: fix permissions of /var/www/html/
  file:
    path: "/var/www/html"
    state: directory
    mode: 0755
    owner: "root"
    group: "root"


- name: enable and start nginx.service
  include_role: name="base/systemd/enable-and-start"
  vars:
    service_name: nginx.service


###########
# AWSTATS #
###########
- name: install awstats
  apt:
    pkg:
    - fcgiwrap
    - awstats
    - libnet-ip-perl
    - libnet-dns-perl
    state: present
    policy_rc_d: 101
  tags: "online"
  ignore_errors: "{{ignore_online_errors | bool}}"


- name: remove broken awstats directory
  file:
    path: "/etc/logrotate.d/httpd-prerotate/awstats"
    state: absent


- name: disable periodic update by cron
  copy:
    content: "# disabled by someone's ansible provisioner"
    dest: "/etc/cron.d/awstats"
    mode: 0640
    owner: "root"
    group: "root"


- name: copy awstats "default" vhost config
  copy:
    src: "{{item}}"
    dest: "/etc/awstats/awstats.conf.local"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/awstats.conf.local"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/awstats.conf.local"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/awstats.conf.local"
    - "default/awstats.conf.local"


- name: copy awstats logrotate script
  copy:
    src: "{{item}}"
    dest: "/etc/logrotate.d/httpd-prerotate/awstats-logrotate-script"
    mode: 0750
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/awstats-logrotate-script"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/awstats-logrotate-script"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/awstats-logrotate-script"
    - "default/awstats-logrotate-script"


- name: nginx logrotate zzz config
  copy:
    src: "{{item}}"
    dest: "/etc/logrotate.d/zzz_nginx"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/nginx.logrotate"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/nginx.logrotate"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/nginx.logrotate"
    - "default/nginx.logrotate"


- name: nginx logrotate empty config
  copy:
    src: "{{item}}"
    dest: "/etc/logrotate.d/nginx"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/nginx.empty.logrotate"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/nginx.empty.logrotate"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/nginx.empty.logrotate"
    - "default/nginx.empty.logrotate"


- name: nginx-awstats pam.d config
  copy:
    src: "{{item}}"
    dest: "/etc/pam.d/nginx-awstats"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/nginx-awstats.pam"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/nginx-awstats.pam"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/nginx-awstats.pam"
    - "default/nginx-awstats.pam"


- name: enable and start fcgiwrap.socket
  include_role: name="base/systemd/enable-and-start"
  vars:
    service_name: fcgiwrap.socket
