16.5. 使用 logging 系统角色应用远程日志解决方案

按照以下步骤准备并应用 Red Hat Ansible Core playbook 来配置远程日志记录解决方案。在本 playbook 中,一个或多个客户端从 systemd-journal 获取日志,并将它们转发到远程服务器。服务器从 remote_rsyslogremote_files 接收远程输入,并将日志输出到由远程主机名命名的目录中的本地文件。

先决条件

注意

您不必安装 rsyslog 软件包,因为系统管理员在部署时会安装 rsyslog

流程

  1. 创建一个 playbook 文件,如包含以下内容的 ~/logging-playbook.yml

    ---
    - name: Deploying remote input and remote_files output
      hosts: server
      roles:
        - rhel-system-roles.logging
      vars:
        logging_inputs:
          - name: remote_udp_input
            type: remote
            udp_ports: [ 601 ]
          - name: remote_tcp_input
            type: remote
            tcp_ports: [ 601 ]
        logging_outputs:
          - name: remote_files_output
            type: remote_files
        logging_flows:
          - name: flow_0
            inputs: [remote_udp_input, remote_tcp_input]
            outputs: [remote_files_output]
    
    - name: Deploying basics input and forwards output
      hosts: clients
      roles:
        - rhel-system-roles.logging
      vars:
        logging_inputs:
          - name: basic_input
            type: basics
        logging_outputs:
          - name: forward_output0
            type: forwards
            severity: info
            target: <host1.example.com>
            udp_port: 601
          - name: forward_output1
            type: forwards
            facility: mail
            target: <host1.example.com>
            tcp_port: 601
        logging_flows:
          - name: flows0
            inputs: [basic_input]
            outputs: [forward_output0, forward_output1]
    
    [basic_input]
    [forward_output0, forward_output1]

    其中 <host1.example.com> 是日志记录服务器。

    注意

    您可以修改 playbook 中的参数以符合您的需要。

    警告

    日志解决方案只适用于在服务器或者客户端系统的 SELinux 策略中定义的端口并在防火墙中打开。默认 SELinux 策略包括端口 601、514、6514、10514 和 20514。要使用其他端口,请修改客户端和服务器系统上的 SELinux 策略

  2. 创建一个列出您的服务器和客户端的新清单文件 inventory.ini

    [servers]
    server ansible_host=<host1.example.com>
    [clients]
    client ansible_host=<host2.example.com>

    其中:

    • <host1.example.com> 是日志记录服务器。
    • <host2.example.com> 是日志记录客户端。
  3. 验证 playbook 语法:

    # ansible-playbook ~/logging-playbook.yml --syntax-check

    请注意,这个命令只验证语法,不会防止错误但有效的配置。

  4. 对清单运行 playbook。

    # ansible-playbook -i ~/inventory.ini ~/logging-playbook.yml

验证

  1. 在客户端和服务器系统上测试 /etc/rsyslog.conf 文件的语法:

    # rsyslogd -N 1
    rsyslogd: version 8.1911.0-6.el8, config validation run (level 1), master config /etc/rsyslog.conf
    rsyslogd: End of config validation run. Bye.
  2. 验证客户端系统向服务器发送信息:

    1. 在客户端系统中发送测试信息:

      # logger test
    2. 在服务器系统上,查看 /var/log/<host2.example.com>/messages 日志,例如:

      # cat /var/log/<host2.example.com>/messages
      Aug  5 13:48:31 <host2.example.com> root[6778]: test

      其中 <host2.example.com> 是客户端系统的主机名。请注意,该日志包含输入 logger 命令的用户的用户名,本例中为 root

其他资源