不,很遗憾,Terraform 没有应用选定的 .tf 文件的功能。 Terraform 应用同一目录中的所有 .tf 文件。
但您可以通过注释掉和取消注释应用所选代码。例如,您在同一目录中有 2 个 .tf 文件 "1st.tf" 和 "2nd.tf" 来为 GCP(Google Cloud平台):
那么,"1st.tf" 的代码如下:
provider "google" {
credentials = file("myCredentials.json")
project = "myproject-113738"
region = "asia-northeast1"
}
resource "google_project_service" "project" {
service = "iam.googleapis.com"
disable_dependent_services = true
}
而 "2nd.tf" 的代码如下:
resource "google_service_account" "service_account_1" {
display_name = "Service Account 1"
account_id = "service-account-1"
}
resource "google_service_account" "service_account_2" {
display_name = "Service Account 2"
account_id = "service-account-2"
}
现在,首先,您只想应用 "1st.tf" 中的代码,因此您需要注释掉 "2nd.tf 中的代码":
1st.tf:
provider "google" {
credentials = file("myCredentials.json")
project = "myproject-113738"
region = "asia-northeast1"
}
resource "google_project_service" "project" {
service = "iam.googleapis.com"
disable_dependent_services = true
}
2nd.tf(注释掉):
# resource "google_service_account" "service_account_1" {
# display_name = "Service Account 1"
# account_id = "service-account-1"
# }
# resource "google_service_account" "service_account_2" {
# display_name = "Service Account 2"
# account_id = "service-account-2"
# }
然后,你申请:
terraform apply -auto-approve
接下来,另外,您想应用 "2nd.tf" 中的代码,因此您需要取消注释 "2nd.tf"中的代码:
1st.tf:
provider "google" {
credentials = file("myCredentials.json")
project = "myproject-113738"
region = "asia-northeast1"
}
resource "google_project_service" "project" {
service = "iam.googleapis.com"
disable_dependent_services = true
}
2nd.tf(取消注释):
resource "google_service_account" "service_account_1" {
display_name = "Service Account 1"
account_id = "service-account-1"
}
resource "google_service_account" "service_account_2" {
display_name = "Service Account 2"
account_id = "service-account-2"
}
然后,你申请:
terraform apply -auto-approve
这样,您可以通过注释掉和取消注释应用所选代码。