Chapter 1
Introduction to OpenShift Virtualization and Ansible
Chapter Summary
# Chapter 1: Introduction to OpenShift Virtualization and Ansible In the modern enterprise, the "Cloud Native" transition is rarely a clean break from the past. While containers offer unparalleled scalability and portability, the reality of production environments involves legacy monoliths, special...
Chapter 1: Introduction to OpenShift Virtualization and Ansible
In the modern enterprise, the "Cloud Native" transition is rarely a clean break from the past. While containers offer unparalleled scalability and portability, the reality of production environments involves legacy monoliths, specialized databases, and third-party appliances that simply cannot—or should not—be containerized yet.
This is where OpenShift Virtualization (based on the upstream KubeVirt project) enters the stage. It allows you to run and manage Virtual Machine (VM) workloads alongside container workloads on a single platform. But managing these VMs at scale using manual clicks in a UI or one-off CLI commands is a recipe for configuration drift. To achieve true operational excellence at an advanced level, we must treat these VMs as code.
In this chapter, we will explore the intersection of OpenShift Virtualization and Ansible, setting the foundation for advanced automation strategies.
1.1 The Shift: From Traditional Hypervisors to KubeVirt
Traditional virtualization (vSphere, Nutanix, etc.) treats VMs as long-lived "pets." OpenShift Virtualization treats them as objects within the Kubernetes ecosystem. When you run a VM on OpenShift, the VM process actually runs inside a Linux container (a Pod).
This architectural shift allows us to apply Kubernetes-native concepts to VMs:
- Declarative State: You define what the VM should look like (CPU, RAM, Disks), and the cluster works to maintain that state.
- Resource Constraints: You can apply Quotas and LimitRanges to VMs just like Pods.
- Unified Networking: VMs can sit on the same Pod network as your microservices, or connect to existing VLANs via Multus.
The Real-World Scenario
Imagine a financial services company migrating a legacy CRM. The frontend is rewritten in Node.js and deployed in containers, but the backend is a massive Microsoft SQL Server instance running on Windows Server. By using OpenShift Virtualization, the team can bring the Windows VM into the same namespace as the Node.js pods, reducing latency and simplifying the security perimeter.
1.2 Why Use Ansible for VM Automation?
While Kubernetes provides the "State," Ansible provides the "Orchestration." Advanced users prefer Ansible for OpenShift Virtualization for several reasons:
- Multi-Tier Orchestration: Ansible can create a VM, wait for it to boot, and then immediately reach inside the guest OS via SSH or WinRM to configure the application.
- Infrastructure as Code (IaC): Ansible playbooks can be version-controlled in Git, providing a clear audit trail of infrastructure changes.
- Bridge to External Systems: Ansible can talk to your CMDB (like ServiceNow), your load balancer (like F5), and your OpenShift cluster in a single workflow.
Key Insight: The true power of OpenShift Virtualization isn't just running VMs on Kubernetes; it’s the ability to apply the same CI/CD pipelines and automation frameworks to legacy workloads that were previously managed by manual "golden image" processes.
1.3 Setting Up Your Automation Environment
To manage OpenShift Virtualization with Ansible, you need the right tools. At an advanced level, we move beyond the
community.kubernetesRequired Collections
Ensure you have the following installed on your control node:
bashansible-galaxy collection install kubernetes.core ansible-galaxy collection install redhat.openshift
Authentication
Ansible interacts with the OpenShift API. You can authenticate using a Kubeconfig file or a Token. For automated pipelines, a ServiceAccount with the
cluster-adminedit1.4 Deep Dive: Defining a VM with Ansible
In OpenShift Virtualization, a VM is defined as a
VirtualMachinerunning: trueVirtualMachineInstanceLet’s look at a practical example. Instead of using raw YAML files, we use the
kubernetes.core.k8sExample: Creating a Linux VM with Cloud-Init
yaml- name: Deploy a RHEL VM in OpenShift kubernetes.core.k8s: state: present definition: apiVersion: kubevirt.io/v1 kind: VirtualMachine metadata: name: rhel-worker-01 namespace: production-vms labels: app: legacy-crm spec: running: true template: spec: domain: cpu: cores: 2 resources: requests: memory: 4Gi devices: disks: - name: containerdisk disk: bus: virtio - name: cloudinitdisk disk: bus: virtio volumes: - name: containerdisk containerDisk: image: quay.io/containerdisks/rhel9:latest - name: cloudinitdisk cloudInitNoCloud: userData: | #cloud-config password: "SecretPassword123" chpasswd: { expire: False } ssh_authorized_keys: - ssh-rsa AAAAB3Nza... user@workstation
Why this is Advanced:
- ContainerDisk: We are pulling the VM image from a container registry. This allows for versioning of the OS image itself.
- Cloud-Init: We are injecting configuration (users, keys) at boot time, eliminating the need for manual post-install steps.
1.5 Advanced VM Lifecycle Management
Managing a VM isn't just about creating it. You need to handle power states, migrations, and snapshots. Using the
redhat.openshiftPractical Tip: Managing Power States
Avoid simply deleting a VM pod to restart it. Use the declarative
runningvirtctlcommandyaml- name: Ensure VM is started kubernetes.core.k8s_patch: kind: VirtualMachine name: rhel-worker-01 namespace: production-vms definition: spec: running: true
Best Practice: DataVolume vs. ContainerDisk
For persistent workloads (databases), always use DataVolumes (via the Storage Modernization Operator/CDI).
- ContainerDisk: Ephemeral, good for stateless workers.
- DataVolume: Persistent, backed by PVCs, supports snapshots and live migration.
1.6 Real-World Best Practices for Advanced Users
- Use Namespace Isolation: Group related VMs into specific namespaces. This allows you to apply NetworkPolicies that restrict VM-to-Pod traffic.
- Leverage Templates: OpenShift provides "Common Templates." Instead of defining a VM from scratch, use Ansible to instantiate a VM from a resource. This ensures the guest OS has the correct optimized settings (e.g., VirtIO drivers).text
Template - Idempotency is King: Always write your Ansible tasks such that running them twice doesn't cause a reboot or a duplicate resource. Use andtext
state: presentto ensure the cluster reaches the desired state before the playbook proceeds.textwait: yes - Monitoring Integration: Advanced automation includes health checks. Use the module in Ansible to check a VM’s application endpoint after the VM reports atext
uristatus in OpenShift.textReady
1.7 Handling Networking at Scale
One of the most complex parts of OpenShift Virtualization is networking. By default, VMs get a Pod IP. For advanced use cases, you often need the VM to have a "real" IP on a corporate VLAN.
You will likely use Multus and Bridge networking. Your Ansible automation should handle the creation of the
NetworkAttachmentDefinitionyaml- name: Create Network Attachment for VLAN 101 kubernetes.core.k8s: state: present definition: apiVersion: "k8s.cni.cncf.io/v1" kind: NetworkAttachmentDefinition metadata: name: vlan101-bridge namespace: production-vms spec: config: '{ "cniVersion": "0.3.1", "type": "bridge", "bridge": "br101", "isGateway": true, "ipam": { "type": "whereabouts", "range": "192.168.101.0/24" } }'
Summary
In this first chapter, we established that OpenShift Virtualization is not just "VMs on OpenShift," but a paradigm shift in how we manage infrastructure. By combining the declarative nature of Kubernetes with the orchestration power of Ansible, we can manage thousands of VMs with the same precision as a single container.
In the next chapter, we will dive deeper into Dynamic Inventory Management, exploring how Ansible can automatically discover your VMs across multiple OpenShift clusters and organize them for massive parallel configuration updates.
Chapter 1 Checklist
- [ ] Installed andtext
kubernetes.corecollections.textredhat.openshift - [ ] Verified access to an OpenShift cluster with the Virtualization Operator installed.
- [ ] Understand the difference between a and atext
VirtualMachine.textVirtualMachineInstance - [ ] Successfully deployed a "Hello World" VM using an Ansible playbook.
What You Learned
- 1
OpenShift Virtualization integrates Virtual Machine and container workloads on a unified platform, enabling seamless management.
- 2
Transitioning VMs to a Kubernetes ecosystem allows for a shift from treating them as 'pets' to treating them as 'cattle', embracing a declarative model.
- 3
Leveraging Ansible with OpenShift Virtualization fosters advanced automation, reducing manual configuration and promoting operational excellence.
📖 Key Terms
An extension of Red Hat OpenShift that adds virtualization capabilities, allowing users to run and manage virtual machines alongside container workloads.
A web-based interface and dashboard for managing Ansible automation, providing features like job scheduling, role-based access control, and monitoring.
An open-source extension for Kubernetes that enables users to run and manage virtual machines and containers in a unified environment.
A YAML file used by Ansible that defines a set of instructions or tasks to automate the configuration and management of resources.
A predefined configuration in virtualization that serves as a blueprint for creating virtual machines with standard settings and applications.
Quiz requires login
Sign in to take the quiz and track your progress.