如何使用 Ansible 的 community.general.redhat_subscription 模块自动化注册 Red Hat Enterprise Linux 8 到 Red Hat Subscription-Manager?以下是一个示例,展示如何使用 Ansible Playbook 注册系统、分配订阅池,并安装软件包。

Ansible 模块 community.general.redhat_subscription 概述

模块名称:community.general.redhat_subscription 功能:管理 Red Hat Subscription Manager (RHSM) 的注册和订阅 该模块是 community.general 集合的一部分,由 Ansible 社区维护。 它通过 subscription-manager 命令管理 RHSM 的注册和订阅,仅适用于 Red Hat Enterprise Linux。

参数介绍

  • state(字符串):present(注册)或 absent(注销)。
  • username(字符串):access.redhat.com 或 Satellite 6 的用户名。
  • password(字符串):access.redhat.com 或 Satellite 6 的密码。
  • auto_attach(布尔值):是否自动消费所有可用的订阅。
  • pool_id(列表):要消费的订阅池 ID 列表。
  • pool(字符串):要消费的订阅池名称,支持正则表达式。
  • consumer_id(字符串):恢复之前的注册状态。

示例:使用 Ansible Playbook 注册 Red Hat Subscription-Manager

Playbook 文件:subscription-manager.yml
---
- name: subscription-manager module demo
  hosts: all
  become: true
  vars:
    subscription_username: "your_username"  # 替换为你的 Red Hat 用户名
    subscription_password: "your_password"  # 替换为你的 Red Hat 密码
  tasks:
    - name: 注册到 subscription-manager
      community.general.redhat_subscription:
        state: present
        username: "{{ subscription_username }}"
        password: "{{ subscription_password }}"
        auto_attach: true

执行命令

$ ansible-playbook -i virtualmachines/demo/inventory register\ RedHat\ subscription/subscription_manager.yml

执行结果

PLAY [subscription-manager module demo] ***********************************
TASK [Gathering Facts] *****************************************************
ok: [demo.example.com]
TASK [注册到 subscription-manager] *****************************************
changed: [demo.example.com]
PLAY RECAP *****************************************************************
demo.example.com           : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

执行前后对比

执行前:

$ ssh devops@demo.example.com
[devops@demo ~]$ sudo su
[root@demo devops]# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.4 (Ootpa)
[root@demo devops]# dnf install nfs-utils
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
No match for argument: nfs-utils
Error: Unable to find a match: nfs-utils
[root@demo devops]# subscription-manager status
+-------------------------------------------+
System Status Details
+-------------------------------------------+
Overall Status: Unknown
System Purpose Status: Unknown
[root@demo devops]# subscription-manager list
+-------------------------------------------+
Installed Product Status
+-------------------------------------------+
Product Name:   Red Hat Enterprise Linux for x86_64
Product ID:     479
Version:        8.4
Arch:           x86_64
Status:         Unknown
Status Details:
Starts:
Ends:
[root@demo devops]# subscription-manager list --available --all
This system is not yet registered. Try 'subscription-manager register --help' for more information.
[root@demo devops]# subscription-manager refresh
This system is not yet registered. Try 'subscription-manager register --help' for more information.
[root@demo devops]#

执行后:

$ ssh devops@demo.example.com
[devops@demo ~]$ sudo su
[root@demo devops]# subscription-manager status
+-------------------------------------------+
System Status Details
+-------------------------------------------+
Overall Status: Disabled
Content Access Mode is set to Simple Content Access. This host has access to content, regardless of subscription status.
System Purpose Status: Disabled
[root@demo devops]# subscription-manager list
+-------------------------------------------+
Installed Product Status
+-------------------------------------------+
Product Name:   Red Hat Enterprise Linux for x86_64
Product ID:     479
Version:        8.4
Arch:           x86_64
Status:         Not Subscribed
Status Details:
Starts:
Ends:
[root@demo devops]# subscription-manager list --available --all
+-------------------------------------------+
Available Subscriptions
+-------------------------------------------+
SKU:                 xxxxxxx
Contract:            xxxxxxxx
Pool ID:             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Provides Management: No
Available:           100
Suggested:           1
Service Type:        L1-L3
Roles:               
Service Level:       Self-Support
Usage:               
Add-ons:             
Subscription Type:   Standard
Starts:              11/11/2021
Ends:                01/10/2022
Entitlement Type:    Physical
Subscription Name:   60 Day Product Trial of Red Hat Ansible Automation Platform, Self-Supported
Provides:            dotNET on RHEL Beta (for RHEL Server)
                     Red Hat Satellite
                     Red Hat CodeReady Linux Builder for x86_64
                     Red Hat Ansible Engine
                     Red Hat Container Images Beta
                     Red Hat Ansible Automation Platform
                     Red Hat Enterprise Linux Atomic Host Beta
                     Red Hat Container Images
                     Red Hat Enterprise Linux High Availability for x86_64
                     Red Hat Single Sign-On
                     dotNET on RHEL (for RHEL Server)
                     Red Hat CodeReady Linux Builder for x86_64 - Extended Update Support
                     Red Hat Enterprise Linux Resilient Storage for x86_64 - Extended Update Support
                     Red Hat Enterprise Linux High Availability for x86_64 - Extended Update Support
                     Red Hat Enterprise Linux Resilient Storage for x86_64
                     Red Hat Software Collections (for RHEL Server)
                     Red Hat Satellite Capsule
                     Red Hat Enterprise Linux Atomic Host
                     Red Hat Developer Tools (for RHEL Server)
                     Red Hat Software Collections Beta (for RHEL Server)
                     Red Hat Enterprise Linux Server
                     Red Hat Developer Tools Beta (for RHEL Server)
                     Red Hat Enterprise Linux for x86_64
                     Red Hat Enterprise Linux for x86_64 - Extended Update Support
                     Red Hat Developer Toolset (for RHEL Server)
SKU:                 xxxxxxx
Contract:            xxxxxxxx
Pool ID:             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Provides Management: No
Available:           100
Suggested:           1
Service Type:        L1-L3
Roles:               
Service Level:       Self-Support
Usage:               
Add-ons:             
Subscription Type:   Standard
Starts:              11/19/2021
Ends:                01/18/2022
Entitlement Type:    Physical
[root@demo devops]# subscription-manager refresh
All local data refreshed
[root@demo devops]# dnf install nfs-utils
Updating Subscription Management repositories.
Last metadata expiration check: 0:02:31 ago on Fri 26 Nov 2021 04:09:10 PM UTC.
Dependencies resolved.
====================================================================================================
 Package                Arch        Version              Repository                          Size
====================================================================================================
Installing:
 nfs-utils              x86_64      1:2.3.3-46.el8       rhel-8-for-x86_64-baseos-rpms      500 k
Installing dependencies:
 gssproxy               x86_64      0.8.0-19.el8         rhel-8-for-x86_64-baseos-rpms      119 k
 keyutils               x86_64      1.5.10-6.el8         rhel-8-for-x86_64-baseos-rpms       63 k
 libverto-libevent      x86_64      0.3.0-5.el8          rhel-8-for-x86_64-baseos-rpms       16 k
 python3-pyyaml         x86_64      3.12-12.el8          rhel-8-for-x86_64-baseos-rpms      193 k
 quota                  x86_64      1:4.04-14.el8        rhel-8-for-x86_64-baseos-rpms      214 k
 quota-nls              noarch      1:4.04-14.el8        rhel-8-for-x86_64-baseos-rpms       95 k
 rpcbind                x86_64      1.2.5-8.el8          rhel-8-for-x86_64-baseos-rpms       70 k
Transaction Summary
====================================================================================================
Install  8 Packages
Total download size: 1.2 M
Installed size: 3.7 M
Is this ok [y/N]: n
Operation aborted.
[root@demo devops]# subscription-manager status
+-------------------------------------------+
    System Status Details
+-------------------------------------------+
Overall Status: Disabled
Content Access Mode is set to Simple Content Access. This host has access to content, regardless of subscription status.
System Purpose Status: Disabled
[root@demo devops]#

注意事项

  1. 认证信息:确保 username 和 password 是有效的 Red Hat 订阅管理凭证。
  2. 自动附加订阅:auto_attach: true 会自动消费所有可用的订阅。如果需要手动指定订阅池,可以使用 pool_id 或 pool 参数。
  3. 安装软件包:注册后,系统将能够访问订阅管理的仓库,从而可以安装软件包。
  4. 幂等性:如果系统已注册,任务将不会重复执行,确保 Playbook 的幂等性。

希望这个示例对您有帮助!