Back to the main page

Ansible managing VM via Oracle Linux Virtualization Manager

Intro

Introduction This brief page reads examples how to use Ansible to retrieve and manage objects in OLVM.

Requirements

This was tested on OL 9. Some dependency yum packages to install are python3-devel, libxml2-devel, libxslt-devel Likely, you'll have a Python virtual environment, this is done with Python 3.10 and Ansible 2.12. Required pip package you need is ovirt-engine-sdk-python
$ pip show ovirt-engine-sdk-python

Name: ovirt-engine-sdk-python
Version: 4.6.1
Summary: Python SDK for oVirt Engine API
Requires: pycurl, six
You also need Ansible Collection ovirt.ovirt. Install it using ansible-galaxy.
$ ansible-galaxy collection list ovirt.ovirt

Collection  Version
----------- -------
ovirt.ovirt 3.1.2

Playbook

Create the role using ansible-galaxy. This play gets one-time auth token for OLVM API, and use in other tasks to collect info about VMs, hosts, domains (storage), networks, clusters and supported OS. The defaults/main.yml variables are:
# Example, use your values.
ansible_python_interpreter: "path-to-python3.9"
olvm_api: "https://olvm.domain.name/ovirt-engine/api"
username: "your-username@domain"
passwd: "password"
The tasks/main.yml
---
- name: Get OLVM token for later use
  ovirt.ovirt.ovirt_auth:
    url: "{{ olvm_api }}"
    username: "{{ username }}"
    password: "{{ passwd }}"
  register: _auth_token

# Previous task returns ansible fact ovirt_auth
# use it as auth={{ ovirt_auth }}
- name: Debug
  ansible.builtin.debug:
    msg: "{{ _auth_token }}"

- name: Get VMs
  ovirt.ovirt.ovirt_vm_info:
    auth: "{{ ovirt_auth }}"
    pattern: "name=*"
  register: _vm

- name: Print VMs
  # list of dicts, print only value for key 'fqdn'
  ansible.builtin.debug:
    msg:
      - "{{ _vm.ovirt_vms | map(attribute='fqdn') }}"

- name: Get hosts
  ovirt.ovirt.ovirt_host_info:
    auth: "{{ ovirt_auth }}"
    pattern: "name=*"
  register: _host

- name: Print hosts
  ansible.builtin.debug:
    msg: "{{ _host.ovirt_hosts | map(attribute='address') }}"

- name: Get domains (storage)
  ovirt.ovirt.ovirt_storage_domain_info:
    auth: "{{ ovirt_auth }}"
    pattern: "name=*"
  register: _domain

- name: Print domains, which is storage
  ansible.builtin.debug:
    msg:
      # query more attributes
      - "{{ _domain.ovirt_storage_domains | json_query('[].{storage: storage, name: name}') }}"

- name: Get networks
  ovirt.ovirt.ovirt_network_info:
    auth: "{{ ovirt_auth }}"
    pattern: "name=*"
  register: _net

- name: Print networks
  ansible.builtin.debug:
    msg: "{{ _net.ovirt_networks | map(attribute='comment') }}"

- name: Get clusters
  ovirt.ovirt.ovirt_cluster_info:
    auth: "{{ ovirt_auth }}"
    pattern: "name=*"
  register: _cluster

- name: Print clusters
  ansible.builtin.debug:
    msg: "{{ _cluster.ovirt_clusters | map(attribute='name')}}"

- name: Get supported OS
  ovirt.ovirt.ovirt_vm_os_info:
    auth: "{{ ovirt_auth }}"
    filter_keys: architecture,name,description
  register: _os

- name: Print supported OS
  ansible.builtin.debug:
    msg: "{{ _os.ovirt_operating_systems }}"
...
In this example, these two tasks create and manage new VM and its boot disk.
---
- name: Manage vm-name
  ovirt.ovirt.ovirt_vm:
    auth: "{{ ovirt_auth }}"
    state: present
    name: "vm-name"
    cluster: "cluster-name"
    storage_domain: "domain-name"
    comment: "my test vm"
    cpu_cores: 2
    disk_format: cow
    memory: 4096MiB
    nics:
      - interface: virtio
        name: "vm-name-nic"

- name: Manage disk for vm-name
  ovirt.ovirt.ovirt_disk:
    auth: "{{ ovirt_auth }}"
    state: present
    name: "vm-name-boot_disk"
    vm_name: vm-name"
    size: 10GiB
    format: cow
    interface: virtio
    storage_domain: "domain-name"


Back to the main page