cli コマンドで tfvar ファイルとして渡す json ファイルに、オブジェクトの 2 つの配列があります。1 つはフレーバーのリストで、もう 1 つはイメージのリストです。私がやりたいのは、フレーバーとイメージごとに VM を作成し、それらを組み合わせることです。
画像配列の例を次に示します。
{
"image_list" : [
{
"ID": "e7d95104-4d15-4087-ab32-4a33f580d664",
"Name": "CentOS-7-x86_64",
"Status": "active"
},
{
"ID": "d266a91a-9bfb-4b32-b0de-f9940adc4017",
"Name": "Debian-8-x86_64",
"Status": "active"
},
{
"ID": "912967ae-4ee8-4aab-ad6d-b97aeeac3526",
"Name": "Debian-9-x86_64",
"Status": "active"
}
]
}
フレーバー リストの例を次に示します。
{
"flavor_list": [
{
"ID": "00000000-0000-000A-0001-000000001024",
"Name": "T1-1vCPU-1GB-RAM",
"RAM": 1024,
"Disk": 50,
"Ephemeral": 0,
"VCPUs": 1,
"Is_Public": true
},
{
"ID": "00000000-0000-000A-0002-000000002048",
"Name": "T1-2vCPU-2GB-RAM",
"RAM": 2048,
"Disk": 50,
"Ephemeral": 0,
"VCPUs": 2,
"Is_Public": true
},
{
"ID": "01829284-4e5e-4bf8-a753-525dc59e8476",
"Name": "M1-4vCPU-32GB-RAM",
"RAM": 32768,
"Disk": 50,
"Ephemeral": 0,
"VCPUs": 4,
"Is_Public": true
}
]
}
https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each/の指示に従いましたが、機能せず、エラー メッセージも役に立ちません。
ここに私のtfファイルがあります:
# Configure the OpenStack Provider
provider "openstack" {
# I have ommited the content here
}
variable "image_list" {
type = list(object({
ID = string
Name = string
Status = string
}))
default = [
{
ID = "912967ae-4ee8-4aab-ad6d-b97aeeac3526",
Name = "Debian-9-x86_64",
Status = "active"
}
]
}
variable "flavor_list" {
type = list(object({
ID = string
Name = string
RAM = number
Disk = number
Ephemeral = number
VCPUs = number
Is_Public = bool
}))
}
# Create a web server
resource "openstack_compute_instance_v2" "Odin_test_VMs" {
for_each = var.flavor_list
dynamic "flavor" {
for_each = var.flavor_list
content {
name = flavor_list.value.Name
}
}
dynamic "image" {
for_each = var.image_list
content {
id = image.value.ID
}
}
name = "VM_${flavor.value.name}"
flavor_name = flavor.value.name
image_id = image.value.id
network {
name = "1234362-network"
uuid = "cb7be82b-e521-4b21-b82c-fb084abe8d3b"
}
}
編集
mjpgpy3 の提案の後でも、同じエラー メッセージが表示されます。
Error: Reference to undeclared resource
on odin_test.tf line 61, in resource "openstack_compute_instance_v2" "Odin_test_VMs":
61: name = "VM_${flavor.value.name}"
A managed resource "flavor" "value" has not been declared in the root module.
Error: Reference to undeclared resource
on odin_test.tf line 62, in resource "openstack_compute_instance_v2" "Odin_test_VMs":
62: flavor_name = flavor.value.name
A managed resource "flavor" "value" has not been declared in the root module.
Error: Reference to undeclared resource
on odin_test.tf line 63, in resource "openstack_compute_instance_v2" "Odin_test_VMs":
63: image_id = image.value.id
A managed resource "image" "value" has not been declared in the root module.