Skip to content

Generate random string in template #2624

Answered by 2-4601
miquecg asked this question in Q&A
Discussion options

You must be logged in to vote

https://masterminds.github.io/sprig/strings.html#randalphanum-randalpha-randnumeric-and-randascii

These four functions generate cryptographically secure (uses crypto/rand) random strings, but with different base character sets:

randAlphaNum uses 0-9a-zA-Z
randAlpha uses a-zA-Z
randNumeric uses 0-9
randAscii uses all printable ASCII characters
Each of them takes one parameter: the integer length of the string.

randNumeric 3

The above will produce a random string with three digits.

$ chezmoi execute-template '{{ randAlpha 10 }}'
yAcuBuYIpo

https://masterminds.github.io/sprig/crypto.html#randbytes

The randBytes function accepts a count N and generates a cryptographically secure (uses cr…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
3 replies
@miquecg
Comment options

@bradenhilton
Comment options

@miquecg
Comment options

Comment options

You must be logged in to vote
2 replies
@halostatue
Comment options

@miquecg
Comment options

Answer selected by miquecg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants