---
-
  name: "Creating Tomcat directories"
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    recurse: true
  with_items:
    - "/etc/tomcat{{ tomcat_version }}/webapps/host-manager/META-INF"
    - "/etc/tomcat{{ tomcat_version }}/webapps/manager/META-INF"

-
  name: "Creating a keystore for the self-signed certificate/private key pair"
  when: selfsigned_enabled
  become_user: "{{ service_user }}"
  community.general.java_keystore:
    name: "{{ settings.hostname }}.{{ settings.domain }}"
    certificate: "{{ lookup('file', '{{ cert_dir }}/crt/{{ settings.hostname }}.{{ settings.domain }}-self-signed.crt') }}"
    private_key: "{{ lookup('file', '{{ cert_dir }}/key/{{ settings.hostname }}.{{ settings.domain }}-self-signed.key') }}"
    password: "{{ cert_pass }}"
    owner: "{{ service_user }}"
    mode: '0777'
    dest: "{{ cert_dir }}/jks/{{ settings.hostname }}.{{ settings.domain }}-self-signed.jks"
  environment:
    PATH: "{{ ansible_env.PATH }}:{{ service_home_dir }}/.sdkman/candidates/java/current/bin"

-
  name: "Creating a keystore for the given certificate/private key pair"
  when: not selfsigned_enabled
  become: true
  become_user: "{{ service_user }}"
  community.general.java_keystore:
    name: "{{ settings.hostname }}.{{ settings.domain }}"
    certificate: "{{ lookup('file', '{{ cert_dir }}/crt/default-signed.crt') }}"
    private_key: "{{ lookup('file', '{{ cert_dir }}/key/default-signed.key') }}"
    password: "{{ cert_pass }}"
    owner: "{{ service_user }}"
    mode: '0777'
    dest: "{{ cert_dir }}/jks/default-signed.jks"
  environment:
    PATH: "{{ ansible_env.PATH }}:{{ service_home_dir }}/.sdkman/candidates/java/current/bin"

-
  name: "Adding group for user {{ tomcat_user }}"
  ansible.builtin.group:
    name: "{{ tomcat_group }}"

-
  name: "Adding user: {{ tomcat_user }}"
  ansible.builtin.user:
    name: "{{ tomcat_user }}"
    group: "{{ tomcat_group }}"
    home: "{{ tomcat_home_dir }}"
    createhome: true
    system: true

-
  name: "Installing Tomcat {{ tomcat_version }}"
  ansible.builtin.apt:
    name:
      - "tomcat{{ tomcat_version }}"
      - "tomcat{{ tomcat_version }}-admin"
      - "tomcat{{ tomcat_version }}-common"
      - "tomcat{{ tomcat_version }}-user"

-
  name: "Starting and Enabling Tomcat {{ tomcat_version }}"
  ansible.builtin.service:
    daemon_reload: true
    name: "tomcat{{ tomcat_version }}"
    state: started
    enabled: true

-
  name: "Setting UI access credentials"
  ansible.builtin.template:
    mode: "0755"
    src: "tomcat-users.xml.j2"
    dest: "/etc/tomcat{{ tomcat_version }}/tomcat-users.xml"
  notify: "Restart tomcat"

-
  name: "Allowing access to Manager and Host Manager apps from any IP"
  ansible.builtin.template:
    mode: "0755"
    src: "context.xml.j2"
    dest: "{{ item }}"
  with_items:
    - "/etc/tomcat{{ tomcat_version }}/context.xml"
  notify: "Restart tomcat"

-
  name: "Configuring Tomcat {{ tomcat_version }}"
  ansible.builtin.template:
    mode: "0755"
    src: "server.xml.j2"
    dest: "{{ item }}"
  with_items:
    - "/etc/tomcat{{ tomcat_version }}/server.xml"
  notify: "Restart tomcat"

-
  name: "Starting and Enabling Tomcat {{ tomcat_version }}"
  ansible.builtin.service:
    daemon_reload: true
    name: "tomcat{{ tomcat_version }}"
    state: restarted
    enabled: true
