У меня есть простой вариант использования: мне нужно создать несколько экземпляров GCE и снабдить их файлом конфигурации.
Я хотел использовать модули, чтобы избежать дублирования кода. Внутри файла модуля есть файл-провайдер для ресурса. Root main.tf даже не нужно знать о провайдерах в файле модуля. Однако этот поставщик никогда не работает. Я написал exec-local инициатора, чтобы проверить, могу ли я запустить инициатора внутри файла модуля, он также не работает. Он не выдает никакой ошибки, terraform просто игнорирует весь этот код для провайдеров. Если я включу эти провайдеры в корневой файл main.tf, то они будут работать, но не в файле main.tf модуля.
Итак, вопрос: как я могу запустить провайдеров внутри файлов модулей? Единственный способ сделать это - создать один огромный файл main.tf и скопировать и вставить один и тот же код несколько раз в файл?
РЕДАКТИРОВАТЬ: Примеры:
Root 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"
}
main.tf модуля "./gce"
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)