Automating Red Hat Virtualization with Red Hat Ansible Automation Platform

Red Hat Virtualization (RHV) is a complete, and fully supported enterprise virtualization platform that is built upon a foundation of Red Hat Enterprise Linux (RHEL), oVirt virtualization management projects, and Kernel-based Virtual Machine (KVM) technology in order to virtualize resources, processes and applications. 

With RHEL as the compute provider, RHV addsan intuitive web interface with a robust API including SDKs for Java, Ruby, Python, JavaScript and Go for management of virtualization instances and resources that comprise a typical datacenter. 

Interacting with an API through a full fledged SDK may present a barrier to datacenter automation due to requisite knowledge of a programming language before getting started. This also means that collaboration may be stifled due to a lack of resources proficient in one of the available SDKs. Standardizing on Ansible for automating RHV allows for all teams and individuals to create and maintain automation without knowledge of a programming language. 

Red Hat Ansible Automation Platform allows for interacting with datacenter services in a cleanly formatted and human readable markup language that offers an on-ramp to automating the datacenter. By leveraging the newly released Ansible Content Collection for RHV, this gentle on-ramp to automation becomes more powerful by leveraging the RHV API to automate all manner of virtualization tasks without the need to first gain proficiency in a programming language.

 

What can the RHV Ansible Content Collection do?

Ansible Content Collections represent the modular approach implemented by Ansible Automation Platform. Among other things, Collections contain modules, which are the pieces of code that get executed against the endpoints being automated. In this case, these modules represent the ability to create RHV datacenters, manage RHV hypervisors, create NICs, disks and VMs, among other tasks. 

By leveraging the modular approach to content distribution afforded by Ansible Content Collections, RHV automation is able to stay up to date even though Ansible Automation Platform and Red Hat Virtualization maintain different lifecycles and release cadences.

 

Where can the RHV Collection be downloaded?

Ansible Automation Platform customers have access to a centralized repository of all certified and supported Collections maintained by Red Hat or jointly supported by trusted partners. Automation Hub lists all available, certified Collections from all vendors. The easiest way to obtain and get started with the RHV Collection is from Automation Hub. This process is also described in RHV documentation.

After installing the Collection, the following requirements on the Ansible control node are required prior to using the RHV Collection:

  • python3-ovirt-engine-sdk4
  • python3-netaddr
  • python3-jmespath
  • python3-passlib

As Collections are distributed and maintained outside of Ansible releases, community members can also use and participate in the development of this Collection upstream from Ansible Galaxy.

Additionally, the RHV engineering team also packages this Collection in RPM format available from the RHV product-specific rpm repository.

Ansible Automation Platform can also be configured to pull Collections directly from Automation Hub using the information covered in “Hands on with Ansible Collections.”

 

What can the RHV Collection be used for?

There are currently 55 modules in this Collection that expose RHV tasks to automation. In addition to modules, this Collection also provides 10 roles that combine multiple modules to automate an entire workflow. For example, the infra role provides the ability to login to the RHV management console and setup RHV infrastructure including: mac pools, data centers, clusters, networks, hosts, users, and groups.

 

RHV automation examples

There are many more advanced use cases for automating RHV using the Ansible Collection, but here’s a simple example that shows how to attach an ISO file uploaded to the storage domain of an existing RHV datacenter.

Here’s an example from RHV documentation on leveraging the python SDK in order to attach an existing ISO to a virtual machine:

# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()

# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]

# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)

# Locate the service that manages the CDROM devices of the virtual machine:
cdroms_service = vm_service.cdroms_service()

# Get the first CDROM:
cdrom = cdroms_service.list()[0]

# Locate the service that manages the CDROM device found in previous step:
cdrom_service = cdroms_service.cdrom_service(cdrom.id)

# Change the CD of the VM to 'my_iso_file.iso'. By default the
# operation permanently changes the disk that is visible to the
# virtual machine after the next boot, but has no effect
# on the currently running virtual machine. If you want to change the
# disk that is visible to the current running virtual machine, change
# the `current` parameter's value to `True`.
cdrom_service.update(
    cdrom=types.Cdrom(
        file=types.File(
            id='my_iso_file.iso'
        ),
    ),
    current=False,
)

print("Attached CD to '%s'." % vm.name())

# Close the connection to the server:
connection.close()

Here’s the same operation of attaching an ISO to a RHV virtual machine using just the RHV Collection:

- name: Change VMs CD
  redhat.rhv.ovirt_vm:
    name: myvm
    cd_iso: drivers.iso

While this example may not highlight a highly complex task, it does demonstrate the ease of use and benefit of using Ansible to quickly accomplish repetitive tasks without adding complexity to automation adoption by requiring in-depth experience with a product-specific SDK.

The RHV Collection can also be used to detach the ISO and boot this virtual machine from the ISO just as easily:

- name: Eject VMs CD
  redhat.rhv.ovirt_vm:
    name: myvm
    cd_iso: ''

- name: Boot VM from CD
  redhat.rhv.ovirt_vm:
    name: myvm
    cd_iso: rhel8_x64.iso
    boot_devices:
        - cdrom

 

Takeaways

Ansible Automation Platform, through the modularity of Ansible Content Collections, is able to provide automation capabilities to many different platforms, operating systems, and third party services. By adopting the Collections standard for distributing automation, the RHV team at Red Hat has benefitted from highly transparent and repeatable automation that can be maintained and delivered outside the release cadence of Ansible!

Now that Ansible Automation Platform is able to deliver RHV resources, take a look at using automation services catalog (included in all Ansible Automation Platform subscriptions) to add self service capabilities to virtual machine provisioning on RHV!

Originally posted on Ansible Blog
Author:

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *