-1

I have created azure automation account with the help of terraform and I have existing runbook PowerShell scripts which I need to import to azure automation account. I am trying for-each loop in the terraform to upload multiple scripts at time but it is not working:

Code:

resource "azurerm_automation_runbook" "example" {
  for_each = fileset(",", "./Azure_Runbooks/*.ps1")
  name                    = split("/", each.value)[1]
  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                 = filemd5("${each.value}")
}

while executing the terraform it is not showing any errors but it is not importing all the scripts which is there in my local. Please suggest me how I can import multiple PowerShell script files to upload to Azure automation account.

Lokesh M
  • 117
  • 1
  • 1
  • 6
  • Sorry, correct code is resource "azurerm_automation_runbook" "example" { for_each = fileset(".", "./Azure_Runbooks/*.ps1") name = split("/", each.value)[1] – Lokesh M May 02 '22 at 11:40
  • after running the script, we are getting below error: – Lokesh M May 02 '22 at 11:41
  • Error: invalid value for name (The name can contain only letters, numbers, underscores and dashes. The name must begin with a letter. The name must be less than 64 characters.) │ │ with module.Oneshareautomationaccount.azurerm_automation_runbook.example["Azure_Runbooks/AssociatePIPToNetworkSecurityGroup.ps1"], │ on AutomationAccount\main.tf line 51, in resource "azurerm_automation_runbook" "example": │ 51: name = split("/", each.value)[1] │ – Lokesh M May 02 '22 at 11:41

1 Answers1

0

We can use code like this to solve your issue.

for_each = fileset("Azure_Runbooks/", "*")

above fileset syntax will import all the script files to azure automation account.

rootShiv
  • 1,375
  • 2
  • 6
  • 21
Lokesh M
  • 117
  • 1
  • 1
  • 6