https://github.com/libre-devops/terraform-external-random-string
A module used to create a random string at a PLAN time (rather than at apply time like random_string)
https://github.com/libre-devops/terraform-external-random-string
Last synced: 11 months ago
JSON representation
A module used to create a random string at a PLAN time (rather than at apply time like random_string)
- Host: GitHub
- URL: https://github.com/libre-devops/terraform-external-random-string
- Owner: libre-devops
- License: mit
- Created: 2025-05-03T18:24:29.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-03T22:07:55.000Z (about 1 year ago)
- Last Synced: 2025-05-14T21:18:20.268Z (about 1 year ago)
- Language: PowerShell
- Size: 24.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
```hcl
###############################################################################
# Detect the OS
###############################################################################
# Full path to the helper that exists only on Windows
locals {
windows_helper = "${abspath(path.module)}\\printf.cmd"
}
# If the helper file exists, we’re on Windows; otherwise assume Linux
data "external" "detect_os" {
program = fileexists(local.windows_helper) ? [local.windows_helper, "{\"os\":\"Windows\"}"] : ["printf", "{\"os\":\"Linux\"}"]
}
locals {
os = data.external.detect_os.result.os
is_windows = lower(local.os) == "windows"
is_linux = lower(local.os) == "linux"
}
###############################################################################
# Generate a random string – one data source per platform
###############################################################################
# Linux branch
data "external" "generate_random_string" {
count = local.is_linux ? 1 : 0
working_dir = var.working_dir == null ? path.module : var.working_dir
program = [
"bash", "-c",
"printf '{\"random_string\":\"%s\"}' \"$(head -c 256 /dev/urandom | tr -dc 'A-Za-z0-9' | head -c ${var.random_string_size})\""
]
}
# Windows branch
data "external" "generate_random_string_windows" {
count = local.is_windows ? 1 : 0
working_dir = var.working_dir == null ? path.module : var.working_dir
program = ["powershell", "-Command", "$randomString = -join ((65..90) + (97..122) | Get-Random -Count ${var.random_string_size} | % {[char]$_}); $json = @{random_string=$randomString} | ConvertTo-Json -Compress; Write-Output $json"]
}
###############################################################################
# Normalise the result
###############################################################################
locals {
random_string = local.is_linux ? lower(data.external.generate_random_string[0].result.random_string) : lower(data.external.generate_random_string_windows[0].result.random_string)
}
```
## Requirements
No requirements.
## Providers
| Name | Version |
|------|---------|
| [external](#provider\_external) | n/a |
## Modules
No modules.
## Resources
| Name | Type |
|------|------|
| [external_external.detect_os](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external) | data source |
| [external_external.generate_random_string](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external) | data source |
| [external_external.generate_random_string_windows](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external) | data source |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| [random\_string\_size](#input\_random\_string\_size) | The size of the random string to generate | `number` | `4` | no |
| [working\_dir](#input\_working\_dir) | The working directory for the module | `string` | `null` | no |
## Outputs
| Name | Description |
|------|-------------|
| [is\_linux](#output\_is\_linux) | True if the OS is Linux |
| [is\_windows](#output\_is\_windows) | True if the OS is Windows |
| [os](#output\_os) | The OS that is running the commands |
| [random\_string](#output\_random\_string) | A random string generated by the external data source |