print(“Hello, World!”)

Musings on code, coders and the universe

Working with Red Hat Linux (RHEL) in a Vagrant Box

In this post I will take you through the first steps of setting up Red Hat Enterprise Linux for modern development using Vagrant. Big companies like buying Linux from other big companies. There’s nothing wrong with that, but when you start working towards treating your Infrastructure as Code the fact that you need a valid subscription with the OS vendor to install any packages makes for a rough start in infrastructure automation. This short tutoail will help you overcome these issues and get Vagrant working with RHEL 7.4.

Creating your developer account

Luckily, Red Hat is giving away subscriptions to RHEL for free if you are a registered developer. So the first thing we need to do is go to the Red Hat developer portal and sign up for the Red Hat developer program.

After confirming your E-Mail Address, go to subscription management and check that the “Red Hat Enterprise Linux Developer Suite” has been added to your subscription inventory:

Screenshot of the RHEL subscription management interface

Create your Vagrant project

The account you just created allows you to download the latest DVD images of RHEL at https://developers.redhat.com/downloads/. You can either use these to create your own Vagrant Box (more effort, but worth it if you are serious about automation) or you can use a pre-built box such as samdoran/rhel7. If you want to build your own RHEL box, check out this handy guide at Wharton University and proceed with this guide when you finished all the steps.

The next step is to create a Vagrantfile by invoking vagrant init samdoran/rhel7. Now if you boot this Vagrant box and try to install a package using yum install you will most like see that the package is not available, since you can only access the EPEL but not the default repository from Red Hat:

[vagrant@rhel7 ~]$ sudo yum install java-1.8.0-openjdk-devel.x86_64
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
No package java-1.8.0-openjdk-devel.x86_64 available.
Error: Nothing to do

Sign into your subscription - automatically

To get this to work we will configure the Vagrant Box to sign into the Red Hat subscription automatically and unregister the virtual machine automatically when you run vagrant destroy so that it does end up consuming one of your 100 allowed installs. I am using the vagrant-triggers plugin for that last part.

Let’s edit the Vagrantfile you just created like this:

# -*- mode: ruby -*-
# vi: set ft=ruby :

missing_plugins = %w'vagrant-triggers'.reject { |p| Vagrant.has_plugin?(p) }
unless missing_plugins.empty?
  system "vagrant plugin install #{missing_plugins.join(' ')}"
  puts "Installed new Vagrant plugins. Please run command again"
  exit 1
end

$user = `lpass show --username -G redhat.com` # Maybe insert your own username here
$password = `lpass show --password -G redhat.com` # Maybe insert your own password here

$script = %{
if ! subscription-manager status; then
  sudo subscription-manager register --username=#{$user.strip} --password=#{$password.strip} --auto-attach
  sudo yum-config-manager --disable rhel-7-server-rt-beta-rpms
fi
}

Vagrant.configure("2") do |config|
  config.vm.box = "samdoran/rhel7"
  config.vm.provision "shell", inline: $script
  config.trigger.before :destroy do
    begin
      run_remote "sudo subscription-manager unregister"
    rescue
      puts "Something went wrong, please remove the machine manually from https://access.redhat.com/management/subscriptions"
    end
  end
end

The code should be pretty self-explanatory. Note that I am using the lastpass-cli to retrieve the username and password for my Red Hat account from my vault. If don’t use a password manager (you should!) replace the command inside the backticks with your username/password. You may need to destroy the machine fist, but the next time you recreate it the provisioning script will sign you into your Red Hat account and you will be able to install any packages you need. Happy automating!