Skip to content
Jonas Andersson edited this page Oct 10, 2017 · 23 revisions

YAML Anchors

Anchors are an easy way to duplicate your content across your setup file. Use & to make an anchor and * to refer to it. You can only use them for dictionaries and values, not lists. The anchor needs to appear above your reference.

Anchor: &my_anchor

Anchor reference: *my_anchor

Example

You designate an anchor with the & character. The ampersand(&) acts as a label, referred to with the * character.

safe_room: &my_safe_room 1234
crossing_training_sorcery_room: *my_safe_room
outfitting_room: *my_safe_room

The next time you update your safe_room, you don't have to update the other settings that refer to it!

Another Example

You can use anchors to merge a set of values into another. You can merge a dictionary into another dictionary.

A dictionary looks like this:

waggle_sets:
  spell_set1:
  spell_set2:
  an_empty_set_of_spells:

You can put attributes in dictionaries. Attributes: name: Shadow noun: Moon book: true attributes_have_a: value

buff_spells:
  spell_1:
    mana: 12
  spell_2:
    mana: 3
    abbrev: spell2
  an_entry:
    in_a_dictionary: true

You can put anchors infront of those spells

buff_spells:
  spell_1: &anchor1
    mana: 12
  spell_2: &another_anchor
    mana: 3
    abbrev: spell2

You can refer to those spells via the anchors.

waggle_sets:
  spell_set1:
    << : *anchor1
  spell_set2:
    << : *another_anchor
    << : *anchor1
    some_other_spell:
      mana: 1
      abbrev: SoS

The above expands into:

waggle_sets:
  spell_set1:
    spell_1: &anchor1
      mana: 12
  spell_set2:
    spell_2: &another_anchor
      mana: 3
      abbrev: spell2
    spell_1: &anchor1
      mana: 12
    some_other_spell:
      mana: 1
      abbrev: SoS

More Examples

https://github.com/rpherbig/dr-scripts/blob/master/profiles/Crannach-setup.yaml#L264

https://github.com/rpherbig/dr-scripts/blob/master/profiles/Crannach-setup.yaml#L309

https://github.com/rpherbig/dr-scripts/blob/aebd500279e7dbf1d56e3f92c1214308be7eee56/profiles/Crannach-setup.yaml#L9

https://github.com/rpherbig/dr-scripts/blob/aebd500279e7dbf1d56e3f92c1214308be7eee56/profiles/Crannach-setup.yaml#L391

Clone this wiki locally