我正在使用Terraform处理Azure。我想以这样的方式构建我的代码,它应该足够模块化以使用一个可从所有目录访问的variable.tf文件(目录将具有特定的VM代码)。假设我有一个common_variable.tf
文件和一些目录,如db_servers,fe_servers,be_servers等。这些目录中的代码将使用common_variable.tf文件,该文件位于所有目录的一级。目前,我的设置是抛出错误,因为它没有在同一目录中找到变量。我该怎么把它链接起来?
ERROR:
$ terraform init
Initializing modules...
- module.frontend_deploy
- module.network
Error getting plugins: module root:
module frontend_deploy.root: 17 error(s) occurred:
* resource 'azurerm_storage_account.storage' config: unknown variable referenced: 'location'. define it with 'variable' blocks
* resource 'azurerm_storage_account.storage' config: unknown variable referenced: 'dns_name'. define it with 'variable' blocks
* resource 'azurerm_network_interface.nic' config: unknown variable referenced: 'location'. define it with 'variable' blocks
* resource 'azurerm_public_ip.external_lb_pip' config: unknown variable referenced: 'location'. define it with 'variable' blocks
* resource 'azurerm_virtual_machine.external_nginx' config: unknown variable referenced: 'location'. define it with 'variable' blocks
* resource 'azurerm_lb.lb' config: unknown variable referenced: 'location'. define it with 'variable' blocks
* resource 'azurerm_lb_rule.lb_rule' config: unknown resource 'azurerm_resource_group.wmic_rg' referenced in variable azurerm_resource_group.wmic_rg.name
* resource 'azurerm_virtual_machine.external_nginx' config: unknown resource 'azurerm_resource_group.wmic_rg' referenced in variable azurerm_resource_group.wmic_rg.name
* resource 'azurerm_virtual_machine.external_nginx' config: unknown resource 'azurerm_availability_set.external_nginx' referenced in variable azurerm_availability_set.external_nginx.id
* resource 'azurerm_lb.lb' config: unknown resource 'azurerm_resource_group.wmic_rg' referenced in variable azurerm_resource_group.wmic_rg.name
* resource 'azurerm_lb_backend_address_pool.backend_pool' config: unknown resource 'azurerm_resource_group.wmic_rg' referenced in variable azurerm_resource_group.wmic_rg.name
* resource 'azurerm_lb_probe.lb_probe' config: unknown resource 'azurerm_resource_group.wmic_rg' referenced in variable azurerm_resource_group.wmic_rg.name
* output 'vm_fqdn': unknown resource 'azurerm_public_ip.lbpip' referenced in variable azurerm_public_ip.lbpip.fqdn
* resource 'azurerm_storage_account.storage' config: unknown resource 'azurerm_resource_group.wmic_rg' referenced in variable azurerm_resource_group.wmic_rg.name
* resource 'azurerm_network_interface.nic' config: unknown resource 'azurerm_subnet.subnet' referenced in variable azurerm_subnet.subnet.id
* resource 'azurerm_network_interface.nic' config: unknown resource 'azurerm_resource_group.wmic_rg' referenced in variable azurerm_resource_group.wmic_rg.name
* resource 'azurerm_public_ip.external_lb_pip' config: unknown resource 'azurerm_resource_group.wmic_rg' referenced in variable azurerm_resource_group.wmic_rg.name
答案 0 :(得分:3)
对于init
命令,您可以使用-backend-config选项配置后端,例如azurerm
。
您可以提供单个值:
terraform init -backend-config="storage_account_name=abcd1234"
或包含后端配置值的文件的路径:
terraform init -backend-config=../shared_config.tfvars
对于plan
或apply
命令,您可以使用-var-file选项指定包含变量的文件:
terraform plan -var-file=../common_variable.tf