【发布时间】:2021-01-26 10:33:29
【问题描述】:
我在使用 terraform 部署虚拟机时运行自定义 shell 脚本,这可能会引发错误。
我的问题是,您如何处理这些错误,因为无论脚本的返回码如何,terraform 总是报告部署成功,这会在 VM 没有执行应有的操作时导致混乱。
这里是用于上下文的 terraform 文件的 sn-p:
data "template_file" "setup_script" {
count = var.agent_count
template = file("scripts/setup.sh")
vars = {
POOL_NAME = var.pool_name
AGENT = "agent-${count.index}"
ORGANIZATION_URL = var.organization_url
TOKEN = var.token
TERRAFORM_VERSION = var.terraform_version
VSTS_AGENT_VERSION = var.vsts_agent_version
}
}
resource "azurerm_linux_virtual_machine" "vmachine" {
count = length(module.network.network_interfaces)
name = "agent-${count.index}"
resource_group_name = azurerm_resource_group.deployment-agents.name
location = azurerm_resource_group.deployment-agents.location
size = "Standard_B1ms"
admin_username = "adminuser"
network_interface_ids = [
module.network.network_interfaces[count.index].id,
]
admin_ssh_key {
username = "adminuser"
public_key = var.ssh_public_key
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
boot_diagnostics {
storage_account_uri = azurerm_storage_account.boot.primary_blob_endpoint
}
custom_data = base64encode(data.template_file.setup_script.*.rendered[count.index])
}
还有setup.sh shell 脚本:
# --- snip ----
apt-get install azure-cli
if [ $? -gt 0 ]; then
echo "Cannot install azure cli!"
exit 1
fi
# Test
exit 1
感谢您的帮助。
【问题讨论】:
标签: azure azure-virtual-machine terraform-provider-azure cloud-init