模块文件中的Terraform供应商

时间:2019-01-09 13:46:17

标签: terraform

我有一个简单的用例:我需要创建几个GCE实例并为它们提供一个配置文件。

我想使用模块来避免代码重复。在模块文件中,有一个资源文件供应器。根目录main.tf甚至不需要了解模块文件中的预配器。但是,此预配器永远无法工作。我编写了一个exec-local资源调配器,以测试是否可以在模块文件中运行资源调配器,但它也不起作用。它不会引发任何错误,terraform只会忽略供应者的所有代码。如果我将这些配置程序包含在root main.tf中,则它们会起作用,但不会出现在模块的main.tf文件中。

问题是:如何在模块文件中运行预配器?唯一的方法是创建一个巨大的main.tf文件,然后在文件中多次复制粘贴相同代码的代码吗?

编辑:示例:

main main.tf

provider "google" {
  credentials = "${file("service_account_key.json")}"
  project     = "some_project_name"
  region      = "europe-west1"
  zone        = "europe-west1-b"
}

module "instance1" {
  source = "./gce"
  name = "micro1"
  machine_type = "f1-micro"
  boot_image = "debian-cloud/debian-9"
  zone = "europe-west1-b"
  ip_address = "10.132.0.31"
}

模块“ ./gce”的main.tf

variable "name" {}
variable "machine_type" {}
variable "boot_image" {}
variable "zone" {}
variable "ip_address" {}

data "template_file" "config" {
  template = "${file("./config.yml")}"
  vars {
    port = "4433"
    ip = "${var.ip_address}"
    test_var = "${var.name}"
  }
}

resource "google_compute_instance" "gce-instance" {
  name = "${var.name}"
  machine_type = "${var.machine_type}"
  zone = "${var.zone}"
  boot_disk {
    initialize_params{
      image = "${var.boot_image}"
      size = "10"
    }
  }

  network_interface {
    network = "default"
    network_ip = "${var.ip_address}"
    access_config {
    }
  }
  provisioner "file" {
    content = "${data.template_file.config.rendered}"
    destination = "/examplePath/config.yml"
    connection {
      type = "ssh"
      user = "someUser"
      private_key = "${file("/somePathToKey/id_rsa")}"
    }
  }
}

提供程序的类型无关紧要,因为我什至尝试了以下方法:

  provisioner "local-exec" {
    command = "echo test >> test.txt"
  }

,它仍然无法运行,预配器无法运行。如果我将预配置程序放入main.tf根目录中,它将起作用,但不能在模块文件中显示。

应用输出:

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + module.instance1.google_compute_instance.gce-instance
      id:                                                  <computed>
      boot_disk.#:                                         "1"
      boot_disk.0.auto_delete:                             "true"
      boot_disk.0.device_name:                             <computed>
      boot_disk.0.disk_encryption_key_sha256:              <computed>
      boot_disk.0.initialize_params.#:                     "1"
      boot_disk.0.initialize_params.0.image:               "debian-cloud/debian-9"
      boot_disk.0.initialize_params.0.size:                "10"
      boot_disk.0.initialize_params.0.type:                <computed>
      can_ip_forward:                                      "false"
      cpu_platform:                                        <computed>
      create_timeout:                                      "4"
      deletion_protection:                                 "false"
      guest_accelerator.#:                                 <computed>
      instance_id:                                         <computed>
      label_fingerprint:                                   <computed>
      machine_type:                                        "f1-micro"
      metadata_fingerprint:                                <computed>
      name:                                                "micro1"
      network_interface.#:                                 "1"
      network_interface.0.access_config.#:                 "1"
      network_interface.0.access_config.0.assigned_nat_ip: <computed>
      network_interface.0.access_config.0.nat_ip:          <computed>
      network_interface.0.access_config.0.network_tier:    <computed>
      network_interface.0.address:                         <computed>
      network_interface.0.name:                            <computed>
      network_interface.0.network:                         "default"
      network_interface.0.network_ip:                      "10.132.0.31"
      network_interface.0.subnetwork_project:              <computed>
      project:                                             <computed>
      scheduling.#:                                        <computed>
      self_link:                                           <computed>
      tags_fingerprint:                                    <computed>
      zone:                                                "europe-west1-b"


Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

module.instance1.google_compute_instance.gce-instance: Creating...
  boot_disk.#:                                         "" => "1"
  boot_disk.0.auto_delete:                             "" => "true"
  boot_disk.0.device_name:                             "" => "<computed>"
  boot_disk.0.disk_encryption_key_sha256:              "" => "<computed>"
  boot_disk.0.initialize_params.#:                     "" => "1"
  boot_disk.0.initialize_params.0.image:               "" => "debian-cloud/debian-9"
  boot_disk.0.initialize_params.0.size:                "" => "10"
  boot_disk.0.initialize_params.0.type:                "" => "<computed>"
  can_ip_forward:                                      "" => "false"
  cpu_platform:                                        "" => "<computed>"
  create_timeout:                                      "" => "4"
  deletion_protection:                                 "" => "false"
  guest_accelerator.#:                                 "" => "<computed>"
  instance_id:                                         "" => "<computed>"
  label_fingerprint:                                   "" => "<computed>"
  machine_type:                                        "" => "f1-micro"
  metadata_fingerprint:                                "" => "<computed>"
  name:                                                "" => "micro1"
  network_interface.#:                                 "" => "1"
  network_interface.0.access_config.#:                 "" => "1"
  network_interface.0.access_config.0.assigned_nat_ip: "" => "<computed>"
  network_interface.0.access_config.0.nat_ip:          "" => "<computed>"
  network_interface.0.access_config.0.network_tier:    "" => "<computed>"
  network_interface.0.address:                         "" => "<computed>"
  network_interface.0.name:                            "" => "<computed>"
  network_interface.0.network:                         "" => "default"
  network_interface.0.network_ip:                      "" => "10.132.0.31"
  network_interface.0.subnetwork_project:              "" => "<computed>"
  project:                                             "" => "<computed>"
  scheduling.#:                                        "" => "<computed>"
  self_link:                                           "" => "<computed>"
  tags_fingerprint:                                    "" => "<computed>"
  zone:                                                "" => "europe-west1-b"
module.instance1.google_compute_instance.gce-instance: Still creating... (10s elapsed)
module.instance1.google_compute_instance.gce-instance: Still creating... (20s elapsed)
module.instance1.google_compute_instance.gce-instance: Creation complete after 21s (ID: micro1)

0 个答案:

没有答案