# 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: Set location
  hosts: localhost
  connection: local
  gather_facts: false
  vars:
    tasktype: Daily
    device: Linux
    report: TestConnection
    actiontype: Report
    ifhostexist: "{{ groups['linux'] | length }}"
  tasks:
  - include_tasks: /etc/ansible/subtasks/report/createreport.yml
    when: ifhostexist | int != 0
- name: Execute main tasks
  hosts: linux
  ignore_unreachable: yes
  gather_facts: false
  strategy: linear
  vars:
    emailrequire:
    templatepath: /etc/ansible/template/linux_ping.j2
  vars_files:
    - /etc/ansible/external/general.yml
  tasks:
  - name: Ping server
    ping:
    register: ping_result
  - name: Set fact ping not ok
    set_fact:
      pingresponse: "Unreachable"
      reason: "{{ping_result.msg}}"
      dateofreport: "{{timenow}}"
    when: ping_result.ping is not defined
    ignore_errors: yes    
  - name: Set fact ping ok
    set_fact:
      pingresponse: "OK"
      dateofreport: "{{timenow}}"
      reason: ''
    when: ping_result.ping is defined
    ignore_errors: yes
  - include_tasks: /etc/ansible/subtasks/report/exportreport.yml
    args:
      apply:
        delegate_to: localhost
    run_once: true
