che yang
che yang

Reputation: 395

Can jsonnet use shell?

I'd like to write code like below in jsonnet. Can jsonnet support this?

local region = `curl http://100.100.100.200/latest/metadata/region_id`

the region variable should be the output of executing curl http://100.100.100.200/latest/metadata/region_id.

Upvotes: 1

Views: 2300

Answers (2)

sbarzowski
sbarzowski

Reputation: 2991

It's not possible to call external commands from within Jsonnet. This is by design (see Hermeticity https://jsonnet.org/articles/design.html).

It's necessary to explicitly pass data to Jsonnet. There are three mechanisms for that:

1) import / importstr which is the best for static things that live alongside the code. But you can use them in other ways (see Kerndog73's answer).

2) External variables - global parameters that are available in the whole program e.g.:

jsonnet --ext-str from_curl="$(curl 'https://example.com')" -e 'std.extVar("from_curl")'

3) Top-level arguments - if your jsonnet program evaluates to a function, you can pass arguments to it:

`jsonnet --tla-str from_curl="$(curl 'https://jsonplaceholder.typicode.com/posts/1')" -e 'function(from_curl) from_curl'`

If you are using ksonnet it may be different, because ksonnet has its own mechanisms for passing data to jsonnet AFAIK.

Upvotes: 1

Indiana Kernick
Indiana Kernick

Reputation: 5341

Fetch your data with curl, pipe it into a file, invoke jsonnet, then local region = importstr “path/to/fetched/data”.

Upvotes: 1

Related Questions