mirror of
https://github.com/kemko/nomad.git
synced 2026-01-03 17:05:43 +03:00
* Move commands from docs to its own root-level directory * temporarily use modified dev-portal branch with nomad ia changes * explicitly clone nomad ia exp branch * retrigger build, fixed dev-portal broken build * architecture, concepts and get started individual pages * fix get started section destinations * reference section * update repo comment in website-build.sh to show branch * docs nav file update capitalization * update capitalization to force deploy * remove nomad-vs-kubernetes dir; move content to what is nomad pg * job section * Nomad operations category, deploy section * operations category, govern section * operations - manage * operations/scale; concepts scheduling fix * networking * monitor * secure section * remote auth-methods folder and move up pages to sso; linkcheck * Fix install2deploy redirects * fix architecture redirects * Job section: Add missing section index pages * Add section index pages so breadcrumbs build correctly * concepts/index fix front matter indentation * move task driver plugin config to new deploy section * Finish adding full URL to tutorials links in nav * change SSO to Authentication in nav and file system * Docs NomadIA: Move tutorials into NomadIA branch (#26132) * Move governance and policy from tutorials to docs * Move tutorials content to job-declare section * run jobs section * stateful workloads * advanced job scheduling * deploy section * manage section * monitor section * secure/acl and secure/authorization * fix example that contains an unseal key in real format * remove images from sso-vault * secure/traffic * secure/workload-identities * vault-acl change unseal key and root token in command output sample * remove lines from sample output * fix front matter * move nomad pack tutorials to tools * search/replace /nomad/tutorials links * update acl overview with content from deleted architecture/acl * fix spelling mistake * linkcheck - fix broken links * fix link to Nomad variables tutorial * fix link to Prometheus tutorial * move who uses Nomad to use cases page; move spec/config shortcuts add dividers * Move Consul out of Integrations; move namespaces to govern * move integrations/vault to secure/vault; delete integrations * move ref arch to docs; rename Deploy Nomad back to Install Nomad * address feedback * linkcheck fixes * Fixed raw_exec redirect * add info from /nomad/tutorials/manage-jobs/jobs * update page content with newer tutorial * link updates for architecture sub-folders * Add redirects for removed section index pages. Fix links. * fix broken links from linkcheck * Revert to use dev-portal main branch instead of nomadIA branch * build workaround: add intro-nav-data.json with single entry * fix content-check error * add intro directory to get around Vercel build error * workound for emtpry directory * remove mdx from /intro/ to fix content-check and git snafu * Add intro index.mdx so Vercel build should work --------- Co-authored-by: Tu Nguyen <im2nguyen@gmail.com>
113 lines
3.9 KiB
Plaintext
113 lines
3.9 KiB
Plaintext
---
|
|
layout: docs
|
|
page_title: try - Functions - Configuration Language
|
|
description: |-
|
|
The try function tries to evaluate a sequence of expressions given as
|
|
arguments and returns the result of the first one that does not produce
|
|
any errors.
|
|
---
|
|
|
|
# `try` Function
|
|
|
|
`try` evaluates all of its argument expressions in turn and returns the result
|
|
of the first one that does not produce any errors.
|
|
|
|
This is a special function that is able to catch errors produced when evaluating
|
|
its arguments, which is particularly useful when working with complex data
|
|
structures whose shape is not well-known at implementation time.
|
|
|
|
For example, if some data is retrieved from an external system in JSON or YAML
|
|
format and then decoded, the result may have attributes that are not guaranteed
|
|
to be set. We can use `try` to produce a normalized data structure which has
|
|
a predictable type that can therefore be used more conveniently elsewhere in
|
|
the configuration:
|
|
|
|
```hcl
|
|
locals {
|
|
raw_value = yamldecode("${path.folder}/example.yaml")
|
|
normalized_value = {
|
|
name = tostring(try(local.raw_value.name, null))
|
|
groups = try(local.raw_value.groups, [])
|
|
}
|
|
}
|
|
```
|
|
|
|
With the above local value expressions, configuration elsewhere in the folder
|
|
can refer to `local.normalized_value` attributes without the need to repeatedly
|
|
check for and handle absent attributes that would otherwise produce errors.
|
|
|
|
We can also use `try` to deal with situations where a value might be provided
|
|
in two different forms, allowing us to normalize to the most general form:
|
|
|
|
```hcl
|
|
variable "example" {
|
|
type = any
|
|
}
|
|
|
|
locals {
|
|
example = try(
|
|
[tostring(var.example)],
|
|
tolist(var.example),
|
|
)
|
|
}
|
|
```
|
|
|
|
The above permits `var.example` to be either a list or a single string. If it's
|
|
a single string then it'll be normalized to a single-element list containing
|
|
that string, again allowing expressions elsewhere in the configuration to just
|
|
assume that `local.example` is always a list.
|
|
|
|
This second example contains two expressions that can both potentially fail.
|
|
For example, if `var.example` were set to `{}` then it could be converted to
|
|
neither a string nor a list. If `try` exhausts all of the given expressions
|
|
without any succeeding, it will return an error describing all of the problems
|
|
it encountered.
|
|
|
|
We strongly suggest using `try` only in special local values whose expressions
|
|
perform normalization, so that the error handling is confined to a single
|
|
location in the folder and the rest of the folder can just use straightforward
|
|
references to the normalized structure and thus be more readable for future
|
|
maintainers.
|
|
|
|
The `try` function can only catch and handle _dynamic_ errors resulting from
|
|
access to data that isn't known until runtime. It will not catch errors
|
|
relating to expressions that can be proven to be invalid for any input, such
|
|
as a malformed reference.
|
|
|
|
~> **Warning:** The `try` function is intended only for concise testing of the
|
|
presence of and types of object attributes. Although it can technically accept
|
|
any sort of expression, we recommend using it only with simple attribute
|
|
references and type conversion functions as shown in the examples above.
|
|
Overuse of `try` to suppress errors will lead to a configuration that is hard
|
|
to understand and maintain.
|
|
|
|
## Examples
|
|
|
|
```shell-session
|
|
> local.foo
|
|
{
|
|
"bar" = "baz"
|
|
}
|
|
> try(local.foo.bar, "fallback")
|
|
baz
|
|
> try(local.foo.boop, "fallback")
|
|
fallback
|
|
```
|
|
|
|
The `try` function will _not_ catch errors relating to constructs that are
|
|
provably invalid even before dynamic expression evaluation, such as a malformed
|
|
reference or a reference to a top-level object that has not been declared:
|
|
|
|
```shell-session
|
|
> try(local.nonexist, "fallback")
|
|
|
|
Error: Reference to undeclared local value
|
|
|
|
A local value with the name "nonexist" has not been declared.
|
|
```
|
|
|
|
## Related Functions
|
|
|
|
- [`can`](/nomad/docs/reference/hcl2/functions/conversion/can), which tries evaluating an expression and returns a
|
|
boolean value indicating whether it succeeded.
|