Skip to content

A module that run commands on virtual machines in Azure. It is a programmatic way to use the "Run Command" option in the portal 🌹

License

Notifications You must be signed in to change notification settings

libre-devops/terraform-azurerm-run-vm-command

Repository files navigation

###############################
# main.tf
###############################
locals {
  # Turn the list into a predictable map for for_each
  cmd_map = {
    for idx, cmd in var.commands :
    coalesce(cmd.name, "run-command-${idx + 1}") => cmd
  }
}

#########################################
# Windows Run-Command (only if windows)
#########################################
resource "azurerm_virtual_machine_run_command" "windows" {
  for_each = lower(var.os_type) == "windows" ? local.cmd_map : {}

  name               = each.key
  location           = var.location
  virtual_machine_id = var.vm_id
  tags               = var.tags

  run_as_user     = try(each.value.run_as_user, null)
  run_as_password = try(each.value.run_as_password, null)

  ######################################
  # pick exactly one source
  ######################################
  dynamic "source" {
    for_each = try(each.value.inline, null) != null ? [1] : []
    content { script = each.value.inline }
  }
  dynamic "source" {
    for_each = try(each.value.script_file, null) != null ? [1] : []
    content { script = file(each.value.script_file) }
  }
  dynamic "source" {
    for_each = try(each.value.script_uri, null) != null ? [1] : []
    content { script_uri = each.value.script_uri }
  }

  lifecycle {
    precondition {
      condition = length(compact([
        try(each.value.inline, null),
        try(each.value.script_file, null),
        try(each.value.script_uri, null)
      ])) == 1
      error_message = "Command '${each.key}' must set exactly ONE of inline, script_file, or script_uri."
    }
  }
}

#########################################
# Linux Run-Command (only if linux)
#########################################
resource "azurerm_virtual_machine_run_command" "linux" {
  for_each = lower(var.os_type) == "linux" ? local.cmd_map : {}

  name               = each.key
  location           = var.location
  virtual_machine_id = var.vm_id
  tags               = var.tags

  run_as_user     = try(each.value.run_as_user, null)
  run_as_password = try(each.value.run_as_password, null)

  # identical source logic -------------------------
  dynamic "source" {
    for_each = try(each.value.inline, null) != null ? [1] : []
    content { script = each.value.inline }
  }
  dynamic "source" {
    for_each = try(each.value.script_file, null) != null ? [1] : []
    content { script = file(each.value.script_file) }
  }
  dynamic "source" {
    for_each = try(each.value.script_uri, null) != null ? [1] : []
    content { script_uri = each.value.script_uri }
  }

  lifecycle {
    precondition {
      condition = length(compact([
        try(each.value.inline, null),
        try(each.value.script_file, null),
        try(each.value.script_uri, null)
      ])) == 1
      error_message = "Command '${each.key}' must set exactly ONE of inline, script_file, or script_uri."
    }
  }
}

Requirements

No requirements.

Providers

Name Version
azurerm n/a

Modules

No modules.

Resources

Name Type
azurerm_virtual_machine_run_command.linux resource
azurerm_virtual_machine_run_command.windows resource

Inputs

Name Description Type Default Required
commands One-or-many commands to run on the VM
list(object({
name = optional(string) # extension name; auto when null
inline = optional(string)
script_file = optional(string)
script_uri = optional(string)
run_as_user = optional(string)
run_as_password = optional(string)
}))
n/a yes
location Azure region (same as the VM) string n/a yes
os_type Operating system of the VM: windows | linux string "windows" no
tags Tags applied to every Run-Command resource map(string) {} no
vm_id ID of the VM the commands should run on string n/a yes

Outputs

Name Description
vm_run_command_ids Resource IDs of all azurerm_virtual_machine_run_command objects
vm_run_command_instance_view Instance-view information for each run-command
vm_run_command_locations Azure region where each run-command resource is created
vm_run_command_names Name property of each run-command resource
vm_run_command_script_uris script_uri values for commands defined via script_uri
vm_run_command_scripts Inline script content for commands defined via inline or script_file

About

A module that run commands on virtual machines in Azure. It is a programmatic way to use the "Run Command" option in the portal 🌹

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published