0

I need to pass variables from tf to script. I have already a working solution for Linux (Bash) machine, now I need to re-write it for Windows (Powershell). This is my Linux resource:

    resource "azurerm_virtual_machine_extension" "runner_server" {
  count = length(var.runner_vm_name)
  name                 = "runnerSettings"
  virtual_machine_id   = azurerm_linux_virtual_machine.runner_server[count.index].id
  publisher            = "Microsoft.Azure.Extensions"
  type                 = "CustomScript"
  type_handler_version = "2.0"
  
  protected_settings = <<PROT
  {
      "script": "${base64encode(data.template_file.runner_settings.rendered)}"
  }
  PROT
}

The template file is:

        data "template_file" "runner_settings" {
      template = file("${var.runner_script_file}")
      vars = {
        REGISTRATION_TOKEN  = var.gitlab_runner_reg_token,
        GITLAB_URL          = var.gitlab_url,
        DEPLOY_ENV          = replace(var.resource_name_prefix, "-cicd", ""),
        RUNNER_TAGS    = join(",", var.gitlab_runner_tags)
      }
    }

So, I added the same template for Windows. In ps1 file I read variables this way:

$RunnerTags = "${RUNNER_TAGS}"

And this is my Windows VM extension:

resource "azurerm_virtual_machine_extension" "windows_runner_server_ext" {
  count = length(var.windows_runner_vm_name)
  name                 = "Settings"
  virtual_machine_id   = azurerm_windows_virtual_machine.windows_runner_server[count.index].id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.9"
  settings = <<SETTINGS
    {
      "commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.windows_runner_settings.rendered)}'))  | Out-File -filepath postBuild.ps1\" && powershell -ExecutionPolicy Unrestricted -File postBuild.ps1'"   
    }
SETTINGS
}

But it didn't work, my variables are still not being written from the template.

user15824359
  • 91
  • 1
  • 11

0 Answers0