Automating RHEL Registration and Subscription

Automate Red Hat Enterprise Linux (RHEL) registration and subscription in C2 Platform development environments using Vagrant for seamless access to Red Hat resources.

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 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. 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.

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

In the context of the C2 Platform’s development workflows (see Development Environment ), environments are continuously recreated. While working on challenging automation tasks that require advanced Ansible engineering, this can happen more than 15 times a day. Even for simpler projects, recreating environments is a standard workflow. As a best practice, before committing and pushing changes, you should test them from a clean baseline—either by reverting to a snapshot or creating a new VM—rather than using a modified VM you’ve been working on.

This can be mitigated to some extent by using snapshots, which is straightforward with Vagrant (see Vagrant ), a key part of the C2 Platform’s development approach.

Without automating subscription management, these frequent recreations disrupt the development workflow, reducing productivity and efficiency. Therefore, it is essential in almost all cases to obtain a Red Hat developer account to unlock this capability when using RHEL-based systems.

Solution

To achieve a fully automated process that integrates seamlessly with Vagrant-based development workflows, use the Vagrant plugin vagrant-registration  . Combined with a customized Vagrantfile, this allows Ansible engineers to work without needing to manually handle subscription management.

Examples and Implementation

Using the Rijkswaterstaat (RWS) project as an example, the implementation in the project c2platform/rws/ansible-gis includes a customized Vagrantfile:

  1. The Vagrantfile includes a code snippet for handling registration that utilizes your Red Hat Developer  account credentials using variables RHEL_DEV_ACCOUNT and RHEL_DEV_SECRET.

     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
    

The C2 Platform’s documentation includes a how-to for Vagrant plugins, including the vagrant-registration plugin. See Setup a Development Environment on Ubuntu 22 and specifically the section on Install Vagrant and Vagrant Plugins .

The setup is fully automated. If you don’t configure these credentials, Vagrant will prompt you to confirm if you want to register (y/n). If you choose yes, it will then ask for your credentials. You can also choose not to register, in which case you will miss the ability to access RHEL resources. If you don’t have a Red Hat developer account or don’t configure the environment variables RHEL_DEV_ACCOUNT and RHEL_DEV_SECRET, this prompting process still provides a more convenient way to register than issuing commands manually inside the VM.

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:



Last modified September 1, 2025: guide dev rhel registration PHX-138 (d3a0e70)