44

I see something like

   celerybeat:
     <<: *django

in https://github.com/pydanny/cookiecutter-django example docker files.

What does it mean? I can't google <<: *

J'e
  • 3,014
  • 4
  • 31
  • 55
eugene
  • 39,839
  • 68
  • 255
  • 489

1 Answers1

73

<< and * are both YAML keys (you can also think of them as operators). And one more key related to your question is &.

In YAML, you can define anchors and later use them. For example,

foo: &myanchor
  key1: "val1"
  key2: "val2"

bar: *myanchor

In this code snippet, & defines an anchor names it myanchor, and *myanchor references that anchor. Now both foo and bar have the same keys and values.

<< is called the YAML merge key. You can compare it to class inheritance in OOP (not so accurate, but can help you understand). See below example.

foo: &myanchor
  key1: "val1"
  key2: "val2"

bar:
  << : *myanchor
  key2: "val2-new"
  key3: "val3"

In this code snippet, we merge keys and values from foo to bar but override the key2 to a new value. And we add a new key-value pair to bar.

Now bar has the following value:

{
  "bar": {
     "key1": "val1",
     "key2": "val2-new",
     "key3": "val3"
   }
}
starball
  • 20,030
  • 7
  • 43
  • 238
Yuankun
  • 6,875
  • 3
  • 32
  • 34
  • 1
    I've updated the example to include the `:` after `<<` needed to make it a key. As a minor note, `*` and `&` aren't really keys, they are indicators. The merge key isn't formally a part of YAML, but an can be a useful extension, it is documented at http://yaml.org/type/merge.html – Burt_Harris May 02 '18 at 00:27