terraformopennebula

Open nebula & terraform context block error


i was able to create vm using terraform but… when i use the context block im facing an issue

Error: Unsupported block type

on terraform.tf line 34, in resource “opennebula_template” “mytemplate”: 34: context {

Blocks of type “context” are not expected here. Did you mean to define argument “context”? If so, use the equals sign to assign it a value.

I am adding it exactly as the guide shows in formal terraform docs in here https://registry.terraform.io/providers/OpenNebula/opennebula/latest/docs/resources/virtual_machine

variable "one_endpoint" {}
variable "one_username" {}
variable "one_password" {}
variable "one_flow_endpoint" {}

provider "opennebula" {
  endpoint      = var.one_endpoint
  flow_endpoint = var.one_flow_endpoint
  username      = var.one_username
  password      = var.one_password
}

#########################################################################

resource "opennebula_image" "CentOS7-clone" {
    clone_from_image = 35
    name             = "CentOS7-clone"
    datastore_id     = 1
    persistent       = false
    permissions      = "660"
    group            = "oneadmin"
}

#########################################################################

resource "opennebula_virtual_machine" "demo" {
  count       = 1
  name        = "centos7"
  cpu         = 2
  vcpu        = 2
  memory      = 4096
  group       = "oneadmin"
  permissions = "660"

  context {
    NETWORK      = "YES"
    HOSTNAME     = "$NAME"
    START_SCRIPT ="yum upgrade"
  }

  graphics {
    type   = "VNC"
    listen = "0.0.0.0"
    keymap = "fr"
  }

  os {
    arch = "x86_64"
    boot = "disk0"
  }

  disk {
    image_id = opennebula_image.CentOS7-clone.id
    size     = 10000
    target   = "vda"
    driver   = "qcow2"
  }

  nic {
    model           = "virtio"
    network_id      = 7
    security_groups = [0]
  }

  vmgroup {
    vmgroup_id = 2
    role       = "vm-group"
  }

  tags = {
    environment = "dev"
  }

  timeout = 5
}

Solution

  • you need to define the context block with an equal sign like below:

    context = {
        NETWORK      = "YES"
        HOSTNAME     = "$NAME"
        START_SCRIPT ="yum upgrade"
    }
    

    Omitting the equal sing for defining attributes was only supported in Terraform <0.12 (Terraform 0.12 Compatibility for Providers - Terraform by HashiCorp). We have an issue for updating the documentation in the GitHub repository.