Separating yaml files and passing in variables
Separating yaml files and passing in variables
Is there any way to separate a YAML file and pass in variables to sub files?
For Example:
Parent yaml file:
Fn::Merge:
- !Include /templates/first.yaml, variables: env: staging
- !Include /templates/second.yaml, variables: env: production
first.yaml
file:
first.yaml
First:
Properties:
env: $env
1 Answer
1
This cannot be done in YAML itself, i.e. there is nothing in the
specification that speaks about including sub files or variable
expansion.
It is possible that a program that loads the YAML does something like
this, but there are some problems with the syntax that you indicate.
The $env
looks like a template what you expect to be replaced bystaging
during loading. However it is unclear from your example whether
you always expect a complete scalar node to be replaced, or if this
can be done mid-scalar and if so what the escape mechanism is
(i.e. how to specify something in an included file that results in a
string $env
in your program).
$env
staging
$env
You should IMO explicitly tag scalars that
needs template expansion. You could make your first.yaml
example into
first.yaml
env: !v env
where interpretation of the !v
tag takes the full scalar. And if you
want expansion withing scalars you use a different tag and the more
verbose template option you have been using.
!v
xyz: !t This is a more verbose $env specficication
You might think you no longer need to worry about escaping, because if
there is no tag, the $env
is not interpreted, but that is not the
case: you can still can have scalars where some $...
patterns need
interpreting and other don't.
$env
$...
Your !Include
tag has some problems as well. It is good that you
make things explicit by using a tag. Some programs like Symfony do
this kind of inclusion by magically interpreting special keys and scalar
syntax (but this could be a result of having to work around the rather
incomplete PHP YAML parser the use).
!Include
But your parameter passing is going to provide some problems as this:
!Include /templates/first.yaml, variables: env: staging
is going to be interpreted as if you wrote a single huge key with an embedded comma.
Your YAML is equivalent to
And I don't think that is what you want.
You should probably make the parameter for !Include
an explicit sequence:
!Include
in which case order of parameters is important. Or use mappings for every parameter:
How this all should be actually implemented depends on your programming language and the parser you are using.
Thanks a lot for replying! I've been using npmjs.com/package/cfn-include to compile the yaml file. I'm not sure what you mean by scalars? All I'm trying to do is pass in a string into a sub file. For clarification: I have 2 parent files. One for staging, one for production. The subfile contains an AWS lambda, which has a property both for database_name and environment. So as a way of preventing code duplication, I want staging.yaml and production yaml both to point to that lambda. But pass in different environments such as "staging" and "production."
– John David
Aug 30 at 4:43
A scalar is a basic YAML node that is not a sequence or a mapping (string, datetime, boolean, integer, float etc). I don't know about npmjs nor about compiling (?) YAML files (normally they are parsed). You should expand your question to include information from your comment, include the code you have etc. Given that you did not know about scalars, a term which is all over the YAML specification, you seem to be doing quite a complex thing for your knowledge level, so the "All" in "All I'm trying" is IMO inappropriate.
– Anthon
Aug 30 at 5:21
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of How can I include a YAML file inside another?
– Todd A. Jacobs
Aug 30 at 1:10