Este contenido no está disponible en el idioma seleccionado.

2.14. Example: Creating a Virtual Machine using Python


Virtual machine creation is performed in several steps. The first step, covered here, is to create the virtual machine object itself.

Example 2.13. Creating a virtual machine using Python

This Python example creates a virtual machine named vm1. The virtual machine in this example:
  • Must have 512 MB of memory, expressed in bytes.
    vm_memory = 512 * 1024 * 1024
    Copy to Clipboard Toggle word wrap
  • Must be attached to the Default cluster, and therefore the Default data center.
    vm_cluster = api.clusters.get(name="Default")
    Copy to Clipboard Toggle word wrap
  • Must be based on the default Blank template.
    vm_template = api.templates.get(name="Blank")
    Copy to Clipboard Toggle word wrap
  • Must boot from the virtual hard disk drive.
    vm_os = params.OperatingSystem(boot=[params.Boot(dev="hd")])
    Copy to Clipboard Toggle word wrap
These options are combined into a virtual machine parameter object, before using the add method of the vms collection to create the virtual machine itself.
from ovirtsdk.api import API
from ovirtsdk.xml import params

try:
    api = API (url="https://HOST",
               username="USER@DOMAIN",
               password="PASS",
               ca_file="ca.crt")

    vm_name = "vm1"
    vm_memory = 512 * 1024 * 1024
    vm_cluster = api.clusters.get(name="Default")
    vm_template = api.templates.get(name="Blank")
    vm_os = params.OperatingSystem(boot=[params.Boot(dev="hd")])

    vm_params = params.VM(name=vm_name,
                         memory=vm_memory,
                         cluster=vm_cluster,
                         template=vm_template,
                         os=vm_os)

    try:
        api.vms.add(vm=vm_params)
        print "Virtual machine '%s' added." % vm_name
    except Exception as ex:
        print "Adding virtual machine '%s' failed: %s" % (vm_name, ex)

    api.disconnect()

except Exception as ex:
    print "Unexpected error: %s" % ex
Copy to Clipboard Toggle word wrap
If the add request is successful then the script will output:
Virtual machine 'vm1' added.
Copy to Clipboard Toggle word wrap
Volver arriba
Red Hat logoGithubredditYoutubeTwitter

Aprender

Pruebe, compre y venda

Comunidades

Acerca de la documentación de Red Hat

Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar. Explore nuestras recientes actualizaciones.

Hacer que el código abierto sea más inclusivo

Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.

Acerca de Red Hat

Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.

Theme

© 2025 Red Hat