---
- name: Create group instance
  group:
    name: "{{ tomcat_instance_name }}"
    gid: "{{ tomcat_instance_port }}"

- name: Create user instance
  user:
    name: "{{ tomcat_instance_name }}"
    group: "{{ tomcat_instance_name }}"
    uid: "{{ tomcat_instance_port }}"
    home: "{{ tomcat_instance_root }}/{{ tomcat_instance_name }}"
    shell: '/bin/bash'
    createhome: no

- name: Create home dir
  file:
    path: "{{ tomcat_instance_root }}/{{ tomcat_instance_name }}"
    state: directory
    owner: "{{ tomcat_instance_name }}"
    group: "{{ tomcat_instance_name }}"
    mode: "u=rwx,g=rwxs,o="

- name: Is /etc/aliases present?
  stat:
    path: /etc/aliases
  register: etc_aliases

- name: Set mail alias for user
  lineinfile:
    dest: '/etc/aliases'
    state: present
    line: "{{ tomcat_instance_name }}: {{ tomcat_instance_mail }}"
    regexp: "{{ tomcat_instance_name }}:"
  when: etc_aliases.stat.exists and tomcat_instance_mail is defined
  register: tomcat_instance_mail_alias

- name: Run newaliases
  command: newaliases
  when: tomcat_instance_mail_alias is changed

- name: Enable sudo right
  lineinfile:
    dest: '/etc/sudoers.d/tomcat'
    state: present
    mode: "0440"
    create: yes
    line: "%{{ tomcat_instance_name }} ALL = ({{ tomcat_instance_name }}) SETENV: ALL"
    validate: 'visudo -cf %s'

- name: Enable sudo right for deploy user
  lineinfile:
    dest: '/etc/sudoers.d/tomcat'
    state: present
    mode: "0440"
    create: yes
    line: "{{ tomcat_instance_deploy_user }} ALL = ({{ tomcat_instance_name }}) NOPASSWD: SETENV: ALL"
    validate: 'visudo -cf %s'
  when: tomcat_instance_deploy_user is defined
