16.5. 使用 logging 系统角色应用远程日志解决方案
按照以下步骤准备并应用 Red Hat Ansible Core playbook 来配置远程日志记录解决方案。在本 playbook 中,一个或多个客户端从 systemd-journal
获取日志,并将它们转发到远程服务器。服务器从 remote_rsyslog
和 remote_files
接收远程输入,并将日志输出到由远程主机名命名的目录中的本地文件。
先决条件
- 您已准备好控制节点和受管节点
- 以可在受管主机上运行 playbook 的用户登录到控制节点。
-
用于连接到受管节点的帐户具有
sudo
权限。
您不必安装 rsyslog
软件包,因为系统管理员在部署时会安装 rsyslog
。
流程
创建一个 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 策略。
创建一个列出您的服务器和客户端的新清单文件
inventory.ini
:[servers] server ansible_host=<host1.example.com> [clients] client ansible_host=<host2.example.com>
其中:
-
<host1.example.com>
是日志记录服务器。 -
<host2.example.com>
是日志记录客户端。
-
验证 playbook 语法:
# ansible-playbook ~/logging-playbook.yml --syntax-check
请注意,这个命令只验证语法,不会防止错误但有效的配置。
对清单运行 playbook。
# ansible-playbook -i ~/inventory.ini ~/logging-playbook.yml
验证
在客户端和服务器系统上测试
/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.
验证客户端系统向服务器发送信息:
在客户端系统中发送测试信息:
# logger test
在服务器系统上,查看
/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
。
其他资源
- 准备控制节点和受管节点以使用 RHEL 系统角色
-
与
rhel-system-roles
软件包一起安装的文档在/usr/share/ansible/roles/rhel-system-roles.logging/README.html
- RHEL 系统角色 知识库文章