Azure Microsoft监视代理无法通过Terraform进行配置

时间:2018-11-20 14:21:03

标签: azure terraform terraform-provider-azure

尝试使用Terraform将MMA代理安装为VM部署的一部分。我确定失败是由于我使用的扩展名的语法引起的,但是我没有运气来确定它应该是什么。代理会安装,但不会加入Log Analytics工作区。我将工作区ID和主键存储在Key Vault中,并在执行时将它们传递到Terraform中。从虚拟机上的扩展日志中,它似乎正在获取正确的工作区ID,但我无法确定它是否正确接收了密钥。以下是我使用的Terraform语法:

resource "azurerm_virtual_machine_extension" "lawks-test" {
  name = "MMA_${azurerm_virtual_machine.test.name}"
  location             = "${azurerm_resource_group.test.location}"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_machine_name = "${azurerm_virtual_machine.test.name}"
  publisher            = "Microsoft.EnterpriseCloud.Monitoring"
  type                 = "MicrosoftMonitoringAgent"
  type_handler_version = "1.0"

  settings = <<SETTINGS
        {
          "workspaceId": "${data.terraform_remote_state.corerg.on_workspace_id}"
        }
        SETTINGS

  protected_settings = <<PROTECTED_SETTINGS
        {
          "workspaceKey": "${var.on_laws_key}"
        }
        PROTECTED_SETTINGS

以下是扩展日志文件之一中显示的内容:

11/19/2018 9:43:51 PM +00:00 Managed Service Identity extension (Microsoft.ManagedIdentity.ManagedIdentityExtensionForWindows) not found on this box, automaticManagement will be skipped on this box.
11/19/2018 9:43:51 PM +00:00 HandlerConfig found, default config will be override, CloudType changed to 1
11/19/2018 9:43:51 PM +00:00 GET http://169.254.169.254/metadata/instance?api-version=2017-08-01 with requestId 9a17250a-bfd0-4e4b-b9d3-aa4ceaf9007e
11/19/2018 9:43:52 PM +00:00 azureResourceId from metadata service.
11/19/2018 9:43:52 PM +00:00 automaticManagement not enabled.
11/19/2018 9:43:52 PM +00:00 systemWorkspace provision failed due to AutomaticManagementNotEnabled
11/19/2018 9:43:52 PM +00:00 only configSpecifiedWorkspace available.
11/19/2018 9:43:52 PM +00:00 SettingFile changed, re-apply configuration.
11/19/2018 9:43:52 PM +00:00 Adding workspace /subscriptions/<my_subscription_id>/resourcegroups/resource_group/providers/microsoft.operationalinsights/workspaces/my-workspace.
11/19/2018 9:43:53 PM +00:00 Unknown error during enable command : System.ArgumentException: Value does not fall within the expected range.
   at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
   at CallSite.Target(Closure , CallSite , ComObject , String , String , Int32 )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid4[T0,T1,T2,T3](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at Microsoft.EnterpriseCloud.Monitoring.MicrosoftMonitoringAgent.Extension.MMAConfigHelper.AddCloudWorkspace(String workspaceId, String workspaceKey, Nullable`1 cloudType)
   at Microsoft.EnterpriseCloud.Monitoring.MicrosoftMonitoringAgent.Extension.EnableProgram.Main(String[] args)

2 个答案:

答案 0 :(得分:0)

刚刚测试过,这是一个有效的示例:

resource "azurerm_resource_group" "test" {
  name     = "acctestRG1"
  location = "UK West"
}

resource "azurerm_virtual_network" "test" {
  name                = "acctvn"
  address_space       = ["10.0.0.0/16"]
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
}

resource "azurerm_subnet" "test" {
  name                 = "acctsub"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_network_name = "${azurerm_virtual_network.test.name}"
  address_prefix       = "10.0.2.0/24"
}

resource "azurerm_network_interface" "test" {
  name                = "acctni"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = "${azurerm_subnet.test.id}"
    private_ip_address_allocation = "dynamic"
  }
}

resource "azurerm_virtual_machine" "test" {
  name                  = "acctvm1z"
  location              = "${azurerm_resource_group.test.location}"
  resource_group_name   = "${azurerm_resource_group.test.name}"
  network_interface_ids = ["${azurerm_network_interface.test.id}"]
  vm_size               = "Standard_DS2_v2"

  storage_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter-smalldisk"
    version   = "latest"
  }

  storage_os_disk {
    name              = "myosdisk1"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  os_profile {
    computer_name  = "hostname1"
    admin_username = "testadmin"
    admin_password = "Password1234!qwe"
  }

  os_profile_windows_config {
    provision_vm_agent = "true"
  }
}

resource "azurerm_virtual_machine_extension" "test" {
  name                 = "omsagent"
  location             = "${azurerm_resource_group.test.location}"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_machine_name = "${azurerm_virtual_machine.test.name}"
  publisher            = "Microsoft.EnterpriseCloud.Monitoring"
  type                 = "MicrosoftMonitoringAgent"
  type_handler_version = "1.0"

  settings = <<SETTINGS
        {
          "workspaceId": "workspaceId"
        }
SETTINGS

  protected_settings = <<PROTECTED_SETTINGS
        {
          "workspaceKey": "workspaceKey"
        }
PROTECTED_SETTINGS # NOTICE THIS STARTS EXACTLY AT THE START OF THE STRING
}

如果这没有帮助,则问题在于您如何传递workspaceKey(因此您缺少密钥的一部分或类似的东西)

答案 1 :(得分:0)

感谢那些答复。我通过尝试手动安装发现了问题所在。 azurerm_log_analytics_workspace资源具有2个与ID相关的属性:

id - The Log Analytics Workspace ID

workspace_id - The Workspace (or Customer) ID for the Log Analytics Workspace

我错误地尝试使用“ id”,这是错误的。一旦使用了“ workspace_id”属性,安装就可以正常进行了……无需更改Terraform代码的格式。

我经常发现有关Terraform的一件事是文档。