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

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)

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 |