An Ansible Approach to Registering RHEL Systems

I am constantly creating and deleting virtual machines. These virtual machines often are RHEL systems, and need to be registered with Red Hat’s CDN. While In the past I had a Role that was wrapped into other provisioning playbooks to perform this task, I find that there are enough one-offs to make it useful to do this as a stand alone playbook. Here is how I set it up, including my rational.

I am following the approach I laid out in my post about running your own, personal, Ansible. As such, I need to think in terms of 4 files:

  1. The inventory file.
  2. The variable file that holds the Red Hat credentials
  3. The Playbook.
  4. The shell script that pulls it all together.

Ansible uses host groups to link the set of hosts with the tasks to perform against them. In our case, we only need to perform a single task against a single host. However, it is likely that the host will be used in other playbooks. Thus, we want to keep the name of the host constant, and create a temporary group of hosts that contains the single host we want to register. However, if we decide we want to run the task against multiple, nothing keeps us from doing that. Here’s what I am using for my current inventory file.

    hosts:
        haproxy.home.younglogic.net:
    vars:
        group_all_var: value
    children:   # key order does not matter, indentation does
        new_hosts:
            hosts:
              haproxy.home.younglogic.net

I’ve called the host group new_hosts with the hope that this name is generic enough to work for other initial provisioning tasks, and will not conflict with other tasks. I could have made it specific to System Registration tasks. The idea, though, is that I will manually remove hosts from the new_hosts group once initial provisioning is complete.

I have a variable file ~/ansible/variables/vault.yaml which contains the necessary information to register the server. It looks like this with my password and pool id replaced with placeholders.

---
redhat_user: ayoung@redhat.com
redhat_password: deadbeef1234
redhat_pool_id: feedbabecafe

My playbook is a single task:

---
- hosts: new_hosts
  tasks:
  - name: Subscribe
    redhat_subscription:
      state: present
      username: "{{ redhat_user }}"
      password: "{{ redhat_password }}"
      pool_ids: "{{ redhat_pool_id }}"

Note that the line

  - hosts: new_hosts

Matches the host group set up in the inventory file.

To execute the playbook, I could run the followin command from the bash prompt:

ansible-playbook -i ~/ansible/inventories/home_infra.yaml -e @~/ansible/variables/vault.yml  ~/ansible/playbooks/subscribe.yaml

I created a script in ~/ansible/bin to remember this.

$ cat ~/ansible/bin/home_network_subscribe.sh 
#/bin/sh
ansible-playbook -i ~/ansible/inventories/home_infra.yaml -e @~/ansible/variables/vault.yml  ~/ansible/playbooks/subscribe.yaml

When done, I can remove the new host from the group in the inventory file. If I want to register a new host, I would first add it to the host section of the inventory file, then add that host the to the new_hosts group. This is a little manual, but it will work for one-offs.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.