Automating RHEL Registration and Subscription

Discover how automating Red Hat Enterprise Linux (RHEL) registration and subscription management simplifies access to essential resources in the C2 Platform development environment.

Projects: c2platform/rws/ansible-gis


Automate the registration and unregistration of RHEL machines using Vagrant in conjunction with the vagrant-registration plugin, streamlining development workflows.


Problem

Accessing Red Hat services and software repositories is essential for developers working in Red Hat Enterprise Linux (RHEL) environments. Automating system registration and subscription management processes is key to ensuring uninterrupted access to these resources.

Context

In RHEL 9 environments, an efficient mechanism is needed to register systems with Red Hat Subscription Management (RHSM) and handle subscriptions. This allows developers to install software packages, receive updates, and use Red Hat services seamlessly.

Solution

Using the Red Hat Developer Subscription, developers can manage RHEL 9 system registrations cost-effectively in development environments. This involves registering the system with RHSM and attaching a developer subscription to unlock access to key Red Hat software repositories and services.

To automate this in the C2 Platform development environment, the vagrant-registration  plugin is pre-installed. It enables smooth, automated registration.

Examples and Implementation

The example below shows how to register a RHEL 9 system and manage subscriptions using terminal commands:

subscription-manager register --username <username>
subscription-manager attach

For detailed automation examples, see the c2platform/rws/ansible-gis project. Key points include:

  1. The Vagrantfile includes a code snippet for handling registration. If you configure your Red Hat Developer  account credentials using variables RHEL_DEV_ACCOUNT and RHEL_DEV_SECRET, the setup will be fully automated. If you don’t configure these credentials, Vagrant will prompt you for them each time you create a RHEL-based VM.

    Vagrantfile

    29      if bx['registration'] || false
    30        if ENV['RHEL_DEV_ACCOUNT'].nil? || ENV['RHEL_DEV_SECRET'].nil?
    31          abort 'ERROR: Please set both RHEL_DEV_ACCOUNT and RHEL_DEV_SECRET ' \
    32          'environment variables. Refer to ' \
    33          'https://c2platform.org/docs/guidelines/dev/rhel for more info.'
    34        end
    35        cfg.registration.skip = false
    36        cfg.registration.unregister_on_halt = false
    37        cfg.registration.username = ENV['RHEL_DEV_ACCOUNT']
    38        cfg.registration.password = ENV['RHEL_DEV_SECRET']
    39      end
    
  2. The Vagrantfile.yml includes a rhel9 box definition with a registration flag. This indicates that systems based on this box will be automatically registered upon creation and unregistered upon destruction.

    Vagrantfile.yml

    344  rhel8:
    345    name: generic/rhel8
    346    version: 4.3.12
    347    registration: true
    348  rhel9:
    349    name: generic/rhel9
    350    version: 4.3.12
    351    registration: true
    

This setup ensures that systems created with the rhel9 configuration automatically handle registration and unregistration, streamlining development and maintaining compliance with Red Hat’s subscription management.

Registration and Unregistration in Action:

The following terminal outputs demonstrate the automatic registration and unregistration process when managing a Vagrant box:

  1. Upon executing vagrant up, the system registers with Red Hat, facilitated by the vagrant-registration plugin.
  2. Running vagrant destroy triggers the system’s unregistration, maintaining a clean state and ensuring no unnecessary subscriptions remain active.

Show me

Note the lines below with “Registering box with vagrant-registration…” and “Unregistering box with vagrant-registration…”

[:ansible-gis]└2 master(+15/-1,+1/-1) ± vagrant up gsd-ansible-repo --no-provision
Bringing machine 'gsd-ansible-repo' up with 'virtualbox' provider...
==> gsd-ansible-repo: Importing base box 'generic/rhel9'...
==> gsd-ansible-repo: Matching MAC address for NAT networking...
==> gsd-ansible-repo: Checking if box 'generic/rhel9' version '4.3.12' is up to date...
==> gsd-ansible-repo: Setting the name of the VM: ansible-gis_gsd-ansible-repo_1708418067798_69697
==> gsd-ansible-repo: Fixed port collision for 22 => 2222. Now on port 2206.
==> gsd-ansible-repo: Clearing any previously set network interfaces...
==> gsd-ansible-repo: Preparing network interfaces based on configuration...
    gsd-ansible-repo: Adapter 1: nat
    gsd-ansible-repo: Adapter 2: hostonly
==> gsd-ansible-repo: Forwarding ports...
    gsd-ansible-repo: 22 (guest) => 2206 (host) (adapter 1)
==> gsd-ansible-repo: Running 'pre-boot' VM customizations...
==> gsd-ansible-repo: Booting VM...
==> gsd-ansible-repo: Waiting for machine to boot. This may take a few minutes...
    gsd-ansible-repo: SSH address: 127.0.0.1:2206
    gsd-ansible-repo: SSH username: vagrant
    gsd-ansible-repo: SSH auth method: private key
==> gsd-ansible-repo: Machine booted and ready!
==> gsd-ansible-repo: Registering box with vagrant-registration...
==> gsd-ansible-repo: Checking for guest additions in VM...
==> gsd-ansible-repo: Setting hostname...
==> gsd-ansible-repo: Configuring and enabling network interfaces...
==> gsd-ansible-repo: Mounting shared folders...
    gsd-ansible-repo: /vagrant => /home/ostraaten/git/gitlab/c2/ansible-gis
    gsd-ansible-repo: /arcgis-software-repo => /software/projects/rws
    gsd-ansible-repo: /ansible-dev-collections => /home/ostraaten/git/gitlab/c2/ansible-dev-collections
==> gsd-ansible-repo: Machine not provisioned because `--no-provision` is specified.
θ66° [:ansible-gis]└2 master(+15/-1,+1/-1) ±
[:ansible-gis]└2 master(+15/-1,+1/-1) ± vagrant destroy gsd-ansible-repo
==> gsd-ansible-repo: Unregistering box with vagrant-registration...
    gsd-ansible-repo: Are you sure you want to destroy the 'gsd-ansible-repo' VM? [y/N] y
==> gsd-ansible-repo: Forcing shutdown of VM...
==> gsd-ansible-repo: Destroying VM and associated drives...
[:ansible-gis]└2 master(+15/-1,+1/-1) ±

The following two guides utilize Red Hat boxes with automatic registration: