Basic templating via yaml #1019
-
have config file. hosts:
- ali
- mamad
How can i run gomplate for generate:
BTW it's awesome project but need more examples for simples use cases. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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 So, you could run a command like this: In your template file instead of using {{ range $sn := .config.hosts }}
server {
server_name {{ $sn }};
}
{{ end }} And putting that all together: $ gomplate -c config=config.yaml -f input.tmpl
server {
server_name ali;
}
server {
server_name mamad;
} Let me know if that helps! |
Beta Was this translation helpful? Give feedback.
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:And putting …