An open API service indexing awesome lists of open source software.

https://github.com/libre-devops/terraform-azurerm-run-vm-command

A module that run commands on virtual machines in Azure. It is a programmatic way to use the "Run Command" option in the portal :rose:
https://github.com/libre-devops/terraform-azurerm-run-vm-command

azure azurerm azurerm-terraform-provider module terraform terraform-modules

Last synced: 19 days ago
JSON representation

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

Awesome Lists containing this project

README

        

```hcl
###############################
# 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](#provider\_azurerm) | n/a |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [azurerm_virtual_machine_run_command.linux](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_run_command) | resource |
| [azurerm_virtual_machine_run_command.windows](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_run_command) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| [commands](#input\_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](#input\_location) | Azure region (same as the VM) | `string` | n/a | yes |
| [os\_type](#input\_os\_type) | Operating system of the VM: windows \| linux | `string` | `"windows"` | no |
| [tags](#input\_tags) | Tags applied to every Run-Command resource | `map(string)` | `{}` | no |
| [vm\_id](#input\_vm\_id) | ID of the VM the commands should run on | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| [vm\_run\_command\_ids](#output\_vm\_run\_command\_ids) | Resource IDs of all azurerm\_virtual\_machine\_run\_command objects |
| [vm\_run\_command\_instance\_view](#output\_vm\_run\_command\_instance\_view) | Instance-view information for each run-command |
| [vm\_run\_command\_locations](#output\_vm\_run\_command\_locations) | Azure region where each run-command resource is created |
| [vm\_run\_command\_names](#output\_vm\_run\_command\_names) | Name property of each run-command resource |
| [vm\_run\_command\_script\_uris](#output\_vm\_run\_command\_script\_uris) | script\_uri values for commands defined via script\_uri |
| [vm\_run\_command\_scripts](#output\_vm\_run\_command\_scripts) | Inline script content for commands defined via inline or script\_file |