Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YAML how to reuse single string content

Tags:

yaml

Lets suppose I've this

a: "ABC"
b: *a

I want b to have the same content than a, keep in mind that a is an string, not an array.

is this possible?

like image 541
Arnold Roa Avatar asked Feb 23 '18 03:02

Arnold Roa


People also ask

Can you concatenate strings in YAML?

Concatenating strings and/or variablesThe standard YAML format does not support the notion of string concatenation. Since concatenating string values is a common pattern in easyconfig files, the EasyBuild framework defines the ! join operator to support this.

How do I refer a variable in YAML?

YAML example¶ We can use the ampersand & character to create a named anchor, that we can then reference later on with an asterisk *. Anchor names must not contain the [, ], {, } and , characters. This came in use when I had to create some elasticsearch mappings that were being duplicated all over the place.

What is a YAML anchor?

What Is An Anchor? YAML anchors are a feature which let you identify an item and then reference it elsewhere in your file. Anchors are created using the & sign. The sign is followed by an alias name. You can use this alias later to reference the value following the anchor.


2 Answers

Yes, that's called an alias. You make an Anchor with &anchorname and refer to it with *anchorname

a: &a "ABC"
b: *a
like image 187
tinita Avatar answered Sep 19 '22 23:09

tinita


You can also use an array to define many aliases which is quite succinct.

aliases:
  - &foo "foo"
  - &bar "bar"
test1: *foo
test2: *bar
like image 32
Paul Sturgess Avatar answered Sep 21 '22 23:09

Paul Sturgess