模块创建的所有实例的列表

时间:2018-10-16 01:48:39

标签: google-compute-engine terraform

我有许多与此类似的模块调用

  1 module "gcpue4a1" {
  2   source = "../../../modules/pods"
  3
  4 }

模块在其中创建实例,DNS记录等的地方。

locals {
  gateway_name = "gateway-${var.network_zone}-${var.environment}-1"
}

resource "google_compute_instance" "gateway" {
  name                      = "${local.gateway_name}"
  machine_type              = "n1-standard-8"
  zone                      = "${var.zone}"
  allow_stopping_for_update = true
}

如何遍历通过此模块创建的所有实例的列表。我可以使用实例标签或标签吗?

最后,我想要的是能够遍历列表以导出到可用的清单文件。但是我不确定将资源封装在模块中时如何执行此操作。

通过terraform show,我可以清楚地看到变量的结构。

➜  gcp-us-east4 git:(integration) ✗ terraform show | grep google_compute_instance.gateway -n1
640-  zone = us-east4-a
641:module.screencast-gcp-pod-gcpue4a1-food.google_compute_instance.gateway:
642-  id = gateway-gcpue4a1-food-1
--
--
991-  zone = us-east4-a
992:module.screencast-gcp-pod-gcpue4a2-food.google_compute_instance.gateway:
993-  id = gateway-gcpue4a2-food-1
--
--
1342-  zone = us-east4-a
1343:module.screencast-gcp-pod-gcpue4a3-food.google_compute_instance.gateway:
1344-  id = gateway-gcpue4a3-food-1
--
--
1693-  zone = us-east4-a
1694:module.screencast-gcp-pod-gcpue4a4-food.google_compute_instance.gateway:
1695-  id = gateway-gcpue4a4-food-1

当我明确地说出我想要哪个节点时,etcd清单工作得很好。它下面的总库存没有,我不确定如何解决。

 10   ##Create ETCD Inventory
 11   provisioner "local-exec" {
 12     command =  "echo \"\n[etcd]\n${google_compute_instance.k8s-master.name} ansible_s    sh_host=${google_compute_instance.k8s-master.network_interface.0.address}\" >> kubesp    ray-inventory"
 13   }
 14
 15   ##Create Nodes Inventory
 16   provisioner "local-exec" {
 17     command =  "echo \"\n[kube-node]\" >> kubespray-inventory"
 18   }
 19 #  provisioner "local-exec" {
 20 #    command =  "echo \"${join("\n",formatlist("%s ansible_ssh_host=%s", google_compu    te_instance.gateway.*.name, google_compute_instance.gateway.*.network_interface.0.add    ress))}\" >> kubespray-inventory"
 21 #  }

➜  gcp-us-east4 git:(integration) ✗ terraform apply

Error: resource 'null_resource.ansible-provision' provisioner local-exec (#4): unknown resource 'google_compute_instance.gateway' referenced in variable google_compute_instance.gateway.*.id

1 个答案:

答案 0 :(得分:1)

您可以确保每个模块都添加一个与该模块匹配的label 然后您可以使用gcloud compute instances list并使用过滤器仅显示带有特定标签的标签。

相关问题