我有一個帶有服務配置的 terraform 模塊和 .yaml docker-compose。Yaml 有變數,例如:
version: '3.2'
services:
cadvisor:
image: gcr.io/google-containers/cadvisor:${var.image_version}
container_name: cadvisor
volumes:
- /:/rootfs:ro
- /var/lib/docker:/var/lib/docker:ro
mem_limit: ${var.limit}
mem_reservation: ${var.reservation}
ports:
- 8080:8080
和我的資源:
resource "local_file" "compose" {
content = file("${var.compose_path}/docker-compose.yml")
filename = "${path.module}/foo.bar"
}
這些值在我的模塊中宣告。我將在我的專案中使用這個 docker-compose.yml,例如發送到遠程服務器并在那里運行。如何將變數值從我的模塊傳遞到 yaml?
我試圖宣告值并使用我的 yaml 執行一些操作,例如resource "local_file" 另存為檔案,但 .yaml 仍然沒有值。
uj5u.com熱心網友回復:
templatefile您可以通過使用內置函式 [1]來實作您想要的:
resource "local_file" "compose" {
content = templatefile("${var.compose_path}/docker-compose.yml", {
image_version = var.image_version
limit = var.limit
reservation = var.reservation
})
filename = "${path.module}/docker-compose.yml"
}
這也需要對docker-compose.yml檔案進行細微更改:
version: '3.2'
services:
cadvisor:
image: gcr.io/google-containers/cadvisor:${image_version}
container_name: cadvisor
volumes:
- /:/rootfs:ro
- /var/lib/docker:/var/lib/docker:ro
mem_limit: ${limit}
mem_reservation: ${reservation}
ports:
- 8080:8080
[1] https://www.terraform.io/language/functions/templatefile
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/531216.html
