Can jsonnet use shell?

2.1k Views Asked by At

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.

2

There are 2 best solutions below

0
On

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.

0
On

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