# Upgrade Ubuntu server
- name: Upgrade Apt Packages
  become: true
  ansible.builtin.apt:
    upgrade: yes
    update_cache: yes

# Check if reboot is required
- name: Check if reboot is required
  become: true
  stat:
    path: /var/run/reboot_required
  register: reboot_required

# Send Discord message when reboot is required
- name: Send Discord message
  ansible.builtin.uri:
    url: "https://discord.com/api/webhooks/webhook"
    method: POST
    body_format: json
    body: '{"content": "Reboot reuired on {{ inventory_hostname }}"}'
    headers:
      Content-Type: application/json
    status_code: 204
  when: reboot_required,stat.exists



  # Check Disk Space
  tasks:
    - name: Get disk usage
      ansible.builtin.command: df -h
      register: disk_usage

    - name: Check disk space available
      ansible.builtin.shell: df -h / |awk 'NR==2 {print $5}'
      register: disk_usage

    # Send Discord message
    - name: Send Discord message
      uri:
        url: "https://discord.com/api/webhooks/otherstuff"
        method: POST
        body_format: json
        body: '{"content": "Disk space on {{ inventory_hostname }} is above 80%!"}'
        headers:
          Content-Type: application/json
        status_code: 204
      when: disk_usage.stdout[:-1]| int > 80


