Xen Orchestra Terraform Provider

repository·master·Indexed 41 days ago

https://github.com/vatesfr/terraform-provider-xenorchestra

A Terraform provider for automating Xen Orchestra infrastructure. It allows users to manage resources via configuration files, featuring data sources for cloud configs, hosts, networks, PIFs, pools, resource sets, and storage repositories (SR).

Tokens
14.4K
Snippets
49
Records
68
Agent score
51%

What's inside Xen Orchestra Terraform Provider

  1. Manage Bonded Xen Orchestra networks with xenorchestra_bonded_network

    master

    The xenorchestra_bonded_network resource allows you to manage bonded networks in Xen Orchestra. Bonding combines multiple Physical Interface (PIF) identifiers into a single logical network for redundancy or increased throughput. For detailed networking logic, refer to the XCP-ng networking documentation.

    resource "xenorchestra_bonded_network" "network" {
      name_label = "new network name"
      bond_mode = "active-backup"
      pool_id = data.xenorchestra_host.host1.pool_id
      pif_ids = [
        data.xenorchestra_pif.eth1.id,
        data.xenorchestra_pif.eth2.id,
      ]
    }
  2. Use the xenorchestra_pools data source

    master

    The xenorchestra_pools data source allows you to retrieve a list of XenOrchestra pools based on specific filtering criteria like tags, or sort them by specific fields. This is useful for dynamically discovering pool IDs to use in other resources (e.g., creating a Storage Repository on a specific pool).

    data "xenorchestra_pools" "example" {
      tags = ["terraform-managed"]
    }
    
    data "xenorchestra_sr" "local_storage" {
      name_label = "Local storage"
      pool_id = data.xenorchestra_pools.example.pools[0].id
    }
  3. Filter Xenorchestra storage repositories with xenorchestra_srs

    master

    The xenorchestra_srs data source allows you to query and filter Xenorchestra storage repositories (SRs) based on specific criteria such as name, pool, host, type, or tags. The resulting list of storage repositories can then be used to drive other Terraform resources, such as attaching storage to a VM or host.

    Common use cases include:

    • Retrieving all available SRs in the environment.
    • Finding SRs belonging to a specific pool or host.
    • Filtering by SR type (e.g., iso, lvm, udev).
    • Sorting results by size or usage to find the largest or emptiest storage.
    data "xenorchestra_srs" "all" {
    }
    
    # Example: Get storage repositories of a specific pool
    data "xenorchestra_srs" "pool_srs" {
      pool_id = "your-pool-id"
    }
  4. How to use Cloud-init with Terraform

    master

    Xen Orchestra has a built-in mechanism for templating cloud-init configs using {name} and % tokens. However, because these are applied client-side in the Xen Orchestra UI, they do not work in Terraform.

    To achieve similar functionality in Terraform, use the templatefile function to inject variables into a .tftpl file, and then pass that content to the cloud_config argument of the xenorchestra_vm resource or use the xenorchestra_cloud_config resource.

    resource "xenorchestra_cloud_config" "example" {
      name = "cloud-config-name"
      template = templatefile("cloud_config.tftpl", {
        hostname = "your-hostname"
        domain   = "your.domain.com"
      })
    }
    
    resource "xenorchestra_vm" "example" {
      # ... other arguments ...
      cloud_config = xenorchestra_cloud_config.example.template
    }
  5. Use the xenorchestra_pif data source

    master

    The xenorchestra_pif data source retrieves information about a physical network interface (PIF) on a XenServer host. You can use it to find the network_id associated with a specific device (e.g., eth0) to configure VM networking.

    Important: This data source requires arguments that identify a unique PIF. If the provided criteria match multiple PIFs, the Terraform operation will fail. To ensure uniqueness, combine device, vlan, and host_id where necessary.

    data "xenorchestra_pif" "eth0" {
      device = "eth0"
      vlan   = -1
    }
    
    resource "xenorchestra_vm" "demo-vm" {
      // ...
      network {
        network_id = data.xenorchestra_pif.eth0.network
      }
      // ...
    }
  6. Manage Access Control Lists with xenorchestra_acl

    master

    The xenorchestra_acl resource allows you to define permissions for a specific user (subject) on a specific object (such as a pool) within Xen Orchestra. You specify the level of access using the action argument.

    data "xenorchestra_pool" "pool" {
      name_label = "Your pool"
    }
    
    data "xenorchestra_user" "user" {
      username = "my-username"
    }
    
    resource "xenorchestra_acl" "acl" {
      subject = data.xenorchestra_user.user.id
      object = data.xenorchestra_pool.pool.id
      action = "operator"
    }
  7. Manage Xen Orchestra networks with xenorchestra_network

    master

    The xenorchestra_network resource allows you to create and manage networks within a Xen Orchestra pool. You can define simple private networks or more complex configurations such as VLAN-tagged networks mapped to specific physical interfaces (PIF devices).

    data "xenorchestra_host" "host1" {
      name_label = "Your host"
    }
    
    # Create a single server network private network
    resource "xenorchestra_network" "private_network" {
      name_label = "new network name"
      pool_id = data.xenorchestra_host.host1.pool_id
    }
    
    # Create a network with a 22 VLAN tag from the eth0 device
    resource "xenorchestra_network" "vlan_network" {
      name_label = "new network name"
      pool_id = data.xenorchestra_host.host1.pool_id
      source_pif_device = "eth0"
      vlan = 22
    }
  8. Use the xenorchestra_resource_set data source

    master

    The xenorchestra_resource_set data source allows you to retrieve information about an existing resource set in Xen Orchestra.

    Important: Resource set names must be unique. If multiple resource sets share the same name, the Terraform data source lookup will fail. Ensure your resource set names are unique within your Xen Orchestra environment before using this data source.

    data "xenorchestra_resource_set" "rs" {
      name = "my resource set"
    }
  9. Use the xenorchestra_network data source

    master

    The xenorchestra_network data source allows you to retrieve information about a specific network within a XenServer pool. This is useful for dynamically referencing network IDs when configuring virtual machines or other resources.

    Important: If multiple networks share the same name_label, Terraform will fail. To ensure uniqueness, combine name_label with pool_id or bridge to uniquely identify the target network.

    data "xenorchestra_network" "net" {
      name_label = "Pool-wide network associated with eth0"
    }
    
    resource "xenorchestra_vm" "demo-vm" {
      // ...
      network {
        network_id = data.xenorchestra_network.net.id
      }
    }
  10. Configure the Xen Orchestra provider

    master

    The provider requires a url and supports two authentication methods: API Token or Username/Password.

    Authentication Logic:

    • If a token is provided, it is used for authentication and the username and password are ignored.
    • If no token is provided, the provider falls back to username and password.

    SSL Verification:

    • The insecure option defaults to false. Set it to true to disable SSL verification, which is useful for deployments using self-signed certificates.
    provider "xenorchestra" {
      # Must be ws or wss : "wss://hostname-of-server"
      url      = var.xo_url
    
      # Authentication via token (preferred) or username/password
      username = local.use_token ? null : var.xo_username
      password = local.use_token ? null : var.xo_password
      token    = local.use_token ? var.xo_token : null
    
      # Disable SSL verification for self-signed certificates
      insecure = var.xo_insecure
    }
    
    locals {
      use_token = var.xo_token != null && trimspace(nonsensitive(var.xo_token)) != ""
    }
  11. Install the Xen Orchestra provider

    master

    To use the Xen Orchestra provider, you must declare it in your Terraform configuration's required_providers block. This allows Terraform to download the provider from the registry during terraform init.

    terraform {
      required_providers {
        xenorchestra = {
          source  = "vatesfr/xenorchestra"
          version = ">= 0.35.0, < 1.0.0"
        }
      }
    }
  12. Use the xenorchestra_sr data source to look up storage repositories

    master

    The xenorchestra_sr data source provides information about a Storage Repository (SR) in Xen Orchestra. This is primarily used to retrieve the id of an SR so it can be referenced in other resources, such as xenorchestra_vm disk configurations.

    Important: This data source must return a unique result. If multiple storage repositories match your criteria, Terraform will fail. To ensure uniqueness, combine name_label, pool_id, host_id, and tags to uniquely identify the target SR.

    data "xenorchestra_sr" "local_storage" {
      name_label = "Your storage repository label"
    }
    
    resource "xenorchestra_vm" "demo-vm" {
      // ...
      disk {
          sr_id = data.xenorchestra_sr.local_storage.id
          name_label = "Ubuntu Bionic Beaver 18.04_imavo"
          size = 32212254720
      }
      // ...
    }