Insert variables/constants in YAML file

Viewed 611

Let's say I want to avoid inserting the same value multiple times in a YAML file:

- name: region_1
  inputs:
    tag_name: constant_tag_name
    aws_region: us-east-1

- name: region_2
  inputs:
    tag_name: constant_tag_name
    aws_region: us-west-2
...

So in the above example, I would like to define a variable somewhere above all sections with the value constant_tag_name and mention the variable everywhere the value is the same. If the name is to change, I would want to change it at the top and have it be reflected everywhere. I looked at YAML aliases but they appear to be for a code section, whereas here I only have one variable. Can you guide me?

1 Answers

You should use an alias, like so:

- const_tag: &CT "constant_tag_name"
- name: region_1
  inputs:
    tag_name: *CT
    aws_region: us-east-1
- name: region_2
  inputs:
    tag_name: *CT
    aws_region: us-west-2

Try it out here: yaml-online-parser.appspot.com

Related