---

- name: Check if /tmp is noexec
  ansible.builtin.shell:
    cmd: "cat /etc/fstab | grep -E \" +/tmp\" | grep noexec"
  register: fstab_tmp_noexec
  failed_when: False
  changed_when: False
  check_mode: no

- block:
  - ansible.builtin.set_fact:
      _logstash_custom_tmpdir: "{{ logstash_custom_tmpdir | default(logstash_default_tmpdir, True) | mandatory }}"

  - name: "Create {{ _logstash_custom_tmpdir }}"
    ansible.builtin.file:
      path: "{{ _logstash_custom_tmpdir }}"
      owner: logstash
      group: logstash
      mode: "0755"
      state: directory
    tags:
      - logstash

  - name: change JVM tmpdir
    ansible.builtin.lineinfile:
      dest: /etc/logstash/jvm.options
      line: "-Djava.io.tmpdir={{ _logstash_custom_tmpdir }}"
      regexp: "^-Djava.io.tmpdir="
      insertafter: "## JVM configuration"
    notify:
      - restart logstash
    tags:
      - logstash
  when: (logstash_custom_tmpdir is not none and logstash_custom_tmpdir | length > 0) or fstab_tmp_noexec.rc == 0
