Skip to content

Basic templating via yaml #1019

Answered by hairyhenderson
mhf-ir asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @mhf-ir,

First off, thanks for your kind words - and I agree more simple examples would help - perhaps something you could be willing to contribute?

To use a YAML file as a data source you'll need to use either the --datasource or --context flags, or -d/-c for short. Personally I prefer the --context/c approach when possible (though you can't use it if the data source needs to be read dynamically).

So, you could run a command like this: gomplate -c config=config.yaml -f input.tmpl

In your template file instead of using $hosts (which hadn't been defined yet), you'll need to reference the context:

{{ range $sn := .config.hosts }}
server {
  server_name {{ $sn }};
}
{{ end }}

And putting …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mhf-ir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants