Search code examples
jsonnet

Can I use current date as a Jsonnet variable?


I want to use current date of system (like 2021-03-01) as a variable in jsonnet.


Solution

  • Yes, but you have to pass it to Jsonnet first. So you'll need something like:

    jsonnet --ext-str date="`date "+%Y-%m-%d"`" your_file.jsonnet
    

    Then you can access it in Jsonnet like:

    std.extVar("date")
    

    You can also use top level arguments (TLAs) to a similar effect.

    Either way all external data needs to be passed to Jsonnet explicitly like that. You cannot just run a function in Jsonnet which asks the operating system for date or any other global parameter. This is very much by design and makes it easier to keep track of what your config depends on.

    More info: