- name: Change /etc/hostname
  hostname:
    name: "{{ hostname }}"

- name: Allow TCP 80 for Gophish
  ufw: rule=allow port=80 proto=tcp

- name: Allow TCP 443 for Gophish
  ufw: rule=allow port=443 proto=tcp

- name: Enable ufw
  ufw: state=enabled policy=deny
  when: enable_ufw_firewall

- name: Update APT package cache
  apt: update_cache=yes

- name: Upgrade APT to the latest packages
  apt: upgrade=safe

- name: Ensure postfix is installed (Debian).
  apt: name=postfix state=installed

- name: Update postfix main.cf configuration file
  template:
    src: main.cf.j2
    dest: /etc/postfix/main.cf
    backup: yes
    owner: root
    group: root
    mode: 0644

- name: Restart postfix
  service: name=postfix state=restarted

- name: Ensure postfix is started and enabled at boot.
  service: name=postfix state=started enabled=yes

- name: Download latest Gophish .zip file
  get_url:
    validate_certs: False
    url: https://getgophish.com/releases/latest/linux/64
    dest: /home/{{ gophish_user }}/gophish.zip
    mode: 0755
    owner: "{{ gophish_user }}"
    group: "{{ gophish_user }}"

- name: Unzip gophish file
  unarchive:
    src: /home/{{ gophish_user }}/gophish.zip
    dest: /home/{{ gophish_user }}
    remote_src: True  # File is on target server and not locally.
    owner: "{{ gophish_user }}"
    group: "{{ gophish_user }}"

- shell: ls -d /home/{{ gophish_user }}/gophish-*
  register: gophish_dir

- name: Rename gophish folder
  command: mv {{ item }} /home/{{ gophish_user }}/gophish
  with_items: "{{ gophish_dir.stdout }}"

- name: Change ownership of Gophish folder
  file:
    path: /home/{{ gophish_user }}/gophish
    owner: "{{ gophish_user }}"
    group: "{{ gophish_user }}"
    recurse: True

- name: Add execution privileges to the gophish binary
  file:
    path: /home/{{ gophish_user }}/gophish/gophish
    mode: 0755

- name: Allow gophish binary to bind to privileged ports using setcap
  shell: setcap CAP_NET_BIND_SERVICE=+eip /home/{{ gophish_user }}/gophish/gophish

- name: Copy config.json file
  copy:
    src: files/config.json
    dest: /home/{{ gophish_user }}/gophish/config.json
    owner: "{{ gophish_user }}"
    group: "{{ gophish_user }}"
    mode: 0644

- name: Reboot the box in 1 minute
  command: shutdown -r 1
  when: reboot_box
