第 4 章 使用 RHEL 系统角色监控性能
作为系统管理员,您可以使用 metrics
RHEL 系统角色监控系统的性能。
4.1. 准备一个控制节点和受管节点来使用 RHEL 系统角色
在使用单独的 RHEL 系统角色管理服务和设置前,您必须准备控制节点和受管节点。
4.1.1. 在 RHEL 9 上准备一个控制节点
在使用 RHEL 系统角色前,您必须配置一个控制节点。然后,此系统根据 playbook 从清单中配置受管主机。
前提条件
- 该系统已在客户门户网站中注册。
-
Red Hat Enterprise Linux Server
订阅已附加到系统。 -
可选:
Ansible Automation Platform
订阅被附加到系统上。
流程
创建一个名为
ansible
的用户,来管理并运行 playbook:[root@control-node]# useradd ansible
切换到新创建的
ansible
用户:[root@control-node]# su - ansible
以这个用户身份执行其余步骤。
创建一个 SSH 公钥和私钥:
[ansible@control-node]$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/ansible/.ssh/id_rsa): Enter passphrase (empty for no passphrase): <password> Enter same passphrase again: <password> ...
为密钥文件使用推荐的默认位置。
- 可选: 要防止 Ansible 在每次建立连接时提示您输入 SSH 密钥密码,请配置一个 SSH 代理。
使用以下内容创建
~/.ansible.cfg
文件:[defaults] inventory = /home/ansible/inventory remote_user = ansible [privilege_escalation] become = True become_method = sudo become_user = root become_ask_pass = True
注意~/.ansible.cfg
文件中的设置具有更高的优先级,并覆盖全局/etc/ansible/ansible.cfg
文件中的设置。使用这些设置,Ansible 执行以下操作:
- 管理指定清单文件中的主机。
-
当帐户建立到受管节点的 SSH 连接时,使用
remote_user
参数中设置的帐户。 -
使用
sudo
工具,以root
用户身份在受管节点上执行任务。 - 每次应用 playbook 时,都会提示输入远程用户的 root 密码。出于安全考虑,建议这样做。
创建一个列出受管主机主机名的 INI 或 YAML 格式的
~/inventory
文件。您还可以在清单文件中定义主机组。例如,以下是 INI 格式的清单文件,它有三个主机,以及一个名为US
的主机组:managed-node-01.example.com [US] managed-node-02.example.com ansible_host=192.0.2.100 managed-node-03.example.com
请注意,控制节点必须能够解析主机名。如果 DNS 服务器无法解析某些主机名,请在主机条目旁边添加
ansible_host
参数来指定其 IP 地址。安装 RHEL 系统角色:
在没有 Ansible Automation Platform 的 RHEL 主机上,安装
rhel-system-roles
软件包:[root@control-node]# dnf install rhel-system-roles
此命令在
/usr/share/ansible/collections/ansible_collections/redhat/rhel_system_roles/
目录中安装集合,且ansible-core
软件包作为依赖项。在 Ansible Automation Platform 上,以
ansible
用户身份执行以下步骤:-
在
~/.ansible.cfg
文件中 将 Red Hat Automation hub 定义为内容的主要来源。 从 Red Hat Automation Hub 安装
redhat.rhel_system_roles
集合:[ansible@control-node]$ ansible-galaxy collection install redhat.rhel_system_roles
此命令在
~/.ansible/collections/ansible_collections/redhat/rhel_system_roles/
目录中安装集合。
-
在
后续步骤
- 准备受管节点。如需更多信息,请参阅 准备一个受管节点。
4.1.2. 准备受管节点
受管节点是在清单中列出的系统,它由控制节点根据 playbook 进行配置。您不必在受管主机上安装 Ansible。
前提条件
- 您已准备好了控制节点。如需更多信息,请参阅 在 RHEL 9 上准备一个控制节点。
您从控制节点进行 SSH 访问的权限。
重要以
root
用户身份进行直接的 SSH 访问是一个安全风险。要降低这个风险,您将在此节点上创建一个本地用户,并在准备受管节点时配置一个sudo
策略。然后,控制节点上的 Ansible 可以使用本地用户帐户登录到受管节点,并以不同的用户身份(如root
)运行 playbook。
流程
创建一个名为
ansible
的用户:[root@managed-node-01]# useradd ansible
控制节点稍后使用这个用户建立与这个主机的 SSH 连接。
为
ansible
用户设置密码:[root@managed-node-01]# passwd ansible Changing password for user ansible. New password: <password> Retype new password: <password> passwd: all authentication tokens updated successfully.
当 Ansible 使用
sudo
以root
用户身份执行任务时,您必须输入此密码。在受管主机上安装
ansible
用户的 SSH 公钥:以
ansible
用户身份登录到控制节点,并将 SSH 公钥复制到受管节点:[ansible@control-node]$ ssh-copy-id managed-node-01.example.com /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/ansible/.ssh/id_rsa.pub" The authenticity of host 'managed-node-01.example.com (192.0.2.100)' can't be established. ECDSA key fingerprint is SHA256:9bZ33GJNODK3zbNhybokN/6Mq7hu3vpBXDrCxe7NAvo.
当提示时,输入
yes
进行连接:Are you sure you want to continue connecting (yes/no/[fingerprint])? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
当提示时,输入密码:
ansible@managed-node-01.example.com's password: <password> Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'managed-node-01.example.com'" and check to make sure that only the key(s) you wanted were added.
通过在控制节点上远程执行命令来验证 SSH 连接:
[ansible@control-node]$ ssh managed-node-01.example.com whoami ansible
为
ansible
用户创建sudo
配置:使用
visudo
命令创建并编辑/etc/sudoers.d/ansible
文件:[root@managed-node-01]# visudo /etc/sudoers.d/ansible
与普通编辑器相比,使用
visudo
的好处是,在安装文件前,该工具提供基本的检查,如解析错误。在
/etc/sudoers.d/ansible
文件中配置满足您要求的sudoers
策略,例如:要为
ansible
用户授予权限,以便在输入ansible
用户密码后在此主机上以任何用户和组身份来运行所有命令,请使用:ansible ALL=(ALL) ALL
要向
ansible
用户授予权限,以便在不输入ansible
用户密码的情况下在该主机上以任何用户和组身份来运行所有命令,请使用:ansible ALL=(ALL) NOPASSWD: ALL
或者,配置匹配您安全要求的更精细的策略。有关
sudoers
策略的详情,请查看sudoers (5)
手册页。
验证
验证您可以在所有受管节点上执行来自控制节点的命令:
[ansible@control-node]$ ansible all -m ping BECOME password: <password> managed-node-01.example.com | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" } ...
硬编码的所有组会动态包含清单文件中列出的所有主机。
使用 Ansible
command
模块在所有受管节点上运行whoami
工具来验证特权升级是否正常工作:[ansible@control-node]$ ansible all -m command -a whoami BECOME password: <password> managed-node-01.example.com | CHANGED | rc=0 >> root ...
如果命令返回 root,则您在受管节点上正确地配置了
sudo
。
其他资源
- 在 RHEL 9 上准备一个控制节点
-
sudoers (5)
手册页