- name: Install Nginx
  apt:
    name: "{{ item }}"
    state: present
  loop:
    - nginx

- name: Configure Nginx
  template:
    src: nginx.conf.j2
    dest: /etc/nginx/sites-available/default

- name: Find path to the openssl binary
  command: which openssl
  changed_when: false
  register: which_openssl_output

- name: Register fact containing path to openssl binary
  set_fact:
    dhparam_openssl_binary: "{{ which_openssl_output.stdout_lines[0] }}"
  no_log: true
  check_mode: no

- name: Generate the Diffie-Hellman parameter file if it doesn't exist
  become: yes
  shell: "{{ dhparam_openssl_binary }} dhparam 4096 >> /etc/nginx/dhparam.pem"
  args:
    creates: "/etc/nginx/dhparam.pem"

- name: Start and Enable Nginx
  service:
    name: nginx
    state: started
    enabled: yes

- name: Reload Nginx
  service:
    name: nginx
    state: reloaded
