# Playbook created by NCS Pte Ltd
# Only applicable to use in ancenter system
# Last modified:Thu Mar  2 11:18:49 +08 2023
# Version:1
- name: get fact
  shell: |
    date +"%Y-%m-%d"
    date
    date +"%Y-%m-%d-%H-%M"
  register: mytime
- name: set fact to resuse
  set_fact:
    today: "{{mytime.stdout_lines[0]}}"
    timenow: "{{mytime.stdout_lines[1]}}"
    todaytime: "{{mytime.stdout_lines[2]}}"
- name: set fact to reuse
  set_fact:
    mycsv: "/ancenter/opt-tasks/{{agency}}/{{tasktype}}/{{mytime.stdout_lines[0]}}/{{device}}/{{report}}/{{tasktype}}_tracking_status_{{mytime.stdout_lines[2]}}.csv"
    myreportpath: "/ancenter/opt-tasks/{{agency}}/{{tasktype}}/{{mytime.stdout_lines[0]}}/{{device}}/{{report}}"
  when: tasktype == "Adhoc"
- name: set fact to reuse
  set_fact:
    myreportpath: "/ancenter/opt-tasks/{{agency}}/{{tasktype}}/{{device}}/{{report}}/{{mytime.stdout_lines[0]}}"
    mycsv: "/ancenter/opt-tasks/{{agency}}/{{tasktype}}/{{device}}/{{report}}/{{mytime.stdout_lines[0]}}/{{tasktype}}_tracking_status_{{mytime.stdout_lines[2]}}.csv"
  when: tasktype == "Daily" or tasktype == "Weekly" or tasktype == "Monthly"
- name: set fact to resuse
  set_fact:
    today: "{{mytime.stdout_lines[0]}}"
    timenow: "{{mytime.stdout_lines[1]}}"
    todaytime: "{{mytime.stdout_lines[2]}}"
    tasktype: "{{agency}}_{{tasktype}}"
    reportname: "{{agency}}_{{tasktype}}_{{device}}{{report}}_{{actiontype}}_{{mytime.stdout_lines[2]}}.html"
- name: Ansible check directory.
  stat:
    path: "{{myreportpath}}"
  register: my_folder
- name: "Ansible Create directory if not exists"
  file:
    path: "{{myreportpath}}"
    state: directory
    mode: 0755
    recurse: yes
    owner: ancenter
    group: ancenter
  when: my_folder.stat.exists == false
