---

- name: "server status dirname exists '{{ nginx_serverstatus_suffix_file | dirname }}'"
  file:
    dest: "{{ nginx_serverstatus_suffix_file | dirname }}"
    mode: "0700"
    owner: root
    group: root
    state: directory

- name: set nginx serverstatus suffix if provided
  copy:
    dest: "{{ nginx_serverstatus_suffix_file }}"
    # The last character "\u000A" is a line feed (LF), it's better to keep it
    content: "{{ nginx_serverstatus_suffix }}\u000A"
    force: yes
  when: nginx_serverstatus_suffix | length > 0

- name: generate random string for server-status suffix
  shell: "apg -a 1 -M N -n 1 > {{ nginx_serverstatus_suffix_file }}"
  args:
    creates: "{{ nginx_serverstatus_suffix_file }}"

- name: read nginx server status suffix
  command: "tail -n 1 {{ nginx_serverstatus_suffix_file }}"
  changed_when: False
  check_mode: no
  register: new_nginx_serverstatus_suffix

- name: overwrite nginx_serverstatus_suffix
  set_fact:
    nginx_serverstatus_suffix: "{{ new_nginx_serverstatus_suffix.stdout }}"

- debug:
    var: nginx_serverstatus_suffix
    verbosity: 1
