Lokesh M
Lokesh M

Reputation: 559

azure runbook PowerShell script content is not importing in terraform properly in azure automation account

I have created azure automation account using terraform. I have save my existing runbook PowerShell script files in local. I have successfully uploaded all the script files at one time while creation of automation account with below code:

resource "azurerm_automation_runbook" "example" {
  for_each                = fileset("Azure_Runbooks/", "*")
  name                    = split(".", each.key)[0]
  location                = var.location
  resource_group_name     = var.resource_group
  automation_account_name = azurerm_automation_account.example.name
  log_verbose             = var.log_verbose
  log_progress            = var.log_progress
  runbook_type            = var.runbooktype
  content                 = each.value
}

After running the terraform apply command, all the script files are uploading successfully to the automation account but the content of the PowerShell script is not getting uploaded. I have checked the runbooks in the automation account but there is not content inside the file. I am seeing only the name of the file.

Can some one please help me with above issue.

Upvotes: 0

Views: 526

Answers (2)

Lokesh M
Lokesh M

Reputation: 559

I have fixed the issue with correct code:

resource "azurerm_automation_runbook" "example" {
  for_each                = fileset("Azure_Runbooks/", "*")
  name                    = split(".", each.key)[0]
  location                = var.location
  resource_group_name     = var.resource_group
  automation_account_name = azurerm_automation_account.example.name
  log_verbose             = var.log_verbose
  log_progress            = var.log_progress
  runbook_type            = var.runbooktype
  content                 = file(format("%s%s" , "Azure_Runbooks/" , each.key))
}

Thanks @YoungGova for your help.

Upvotes: 2

YoungGova
YoungGova

Reputation: 63

You assuming fileset(path, pattern) returns the contents of the file as each.value, but that is not the case. The each.value is just the file name.

You need something like:

resource "azurerm_automation_runbook" "example" {
  for_each                = fileset("Azure_Runbooks/", "*")
  name                    = split(".", each.key)[0]
  location                = var.location
  resource_group_name     = var.resource_group
  automation_account_name = azurerm_automation_account.example.name
  log_verbose             = var.log_verbose
  log_progress            = var.log_progress
  runbook_type            = var.runbooktype
  content                 = file(format("%s%s", "Azure_Runbooks/", each.value)
} 

I hope this helps.

Upvotes: 2

Related Questions