---
# host-log-shipping / linux / t0 "Quick start"
#
# Forwards everything rsyslog sees to a collector, with a queue on disk so
# nothing is lost while the collector is down.
#
# Change the two lines under vars, then run:
#   ansible-playbook -i inventory.ini playbook.yml
#
# Verify:
#   ansible all -i inventory.ini -a "logger -t iac-test hello from $(hostname)"
#   Then search your collector for iac-test. It should be there in seconds.
#
# This sends over plain TCP. Use it on a network you control. The Standard
# version adds TLS and is the one to use for anything crossing a boundary.

- name: Forward system logs to a collector
  hosts: all
  become: true
  gather_facts: true

  vars:
    collector_host: logs.example.internal     # change me
    collector_port: 514                       # change me

  tasks:
    - name: Make sure rsyslog is installed
      ansible.builtin.package:
        name: rsyslog
        state: present

    - name: Write the forwarding rule
      ansible.builtin.copy:
        dest: /etc/rsyslog.d/10-forward.conf
        owner: root
        group: root
        mode: "0644"
        content: |
          # Managed by Ansible. Local edits are overwritten on the next run.

          # Keep messages on disk when the collector is unreachable, then send
          # them when it comes back. Without this a collector restart loses
          # every message sent during the outage.
          $ActionQueueType LinkedList
          $ActionQueueFileName fwd_main
          $ActionQueueMaxDiskSpace 512m
          $ActionQueueSaveOnShutdown on
          $ActionResumeRetryCount -1

          *.* @@{{ collector_host }}:{{ collector_port }}
      notify: Restart rsyslog

    - name: Make sure rsyslog is enabled and running
      ansible.builtin.service:
        name: rsyslog
        enabled: true
        state: started

  handlers:
    - name: Restart rsyslog
      ansible.builtin.service:
        name: rsyslog
        state: restarted
