第 2 章 准备一个控制节点和受管节点以使用 RHEL 系统角色
在使用单独的 RHEL 系统角色管理服务和设置前,您必须准备控制节点和受管节点。
2.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/
目录中安装集合。
-
对
后续步骤
- 准备受管节点。如需更多信息,请参阅 准备一个受管节点。