---
# Variable setup.
- name: Include OS-specific variables.
  include_vars: "{{ ansible_os_family | lower }}.yml"

- name: Define nginx_user.
  set_fact:
    nginx_user: "{{ __nginx_user }}"
  when: nginx_user is not defined and ansible_os_family | lower == 'redhat'

# Setup/install tasks.
- name: Ensure required packages are installed.
  package:
    name:
      - "{{ nginx_package_name }}"
    state: present

# Vhost configuration.
- import_tasks: vhosts.yml

# Nginx setup.
- name: Copy nginx configuration in place.
  template:
    src: "{{ nginx_conf_template }}"
    dest: "{{ nginx_conf_file_path }}"
    owner: root
    group: "{{ root_group }}"
    mode: 0644
  notify:
    - reload nginx

- name: Ensure nginx service is running as configured.
  service:
    name: nginx
    state: "{{ nginx_service_state }}"
    enabled: "{{ nginx_service_enabled }}"

- name: Create the webserver files
  file:
    path: "{{ item.root }}"
    state: directory
    owner: "{{ nginx_owner }}"
  loop: "{{ nginx_vhosts }}"
