<p>I have a number of repeated values in my kubernetes yaml file and I wondering if there was a way I could store variables somewhere in the file, ideally at the top, that I can reuse further down</p>
<p>sort of like</p>
<pre><code>variables:
   - appName: &amp;appname myapp
   - buildNumber: &amp;buildno 1.0.23
</code></pre>
<p>that I can reuse further down like</p>
<pre><code>labels:
    app: *appname
    tags.datadoghq.com/version:*buildno
containers:
    - name: *appname

...

image: 123456.com:*buildno

</code></pre>
<p>if those are possible</p>
<p>I know anchors are a thing in yaml I just couldn't find anything on setting variables</p>

Reubeniv
<p>You can't do this in Kubernetes manifests, because you need a processor to manipulate the YAML files. Though you can share the anchors in the same YAML manifest like this:</p>
<pre><code>---
apiVersion: v1
kind: ConfigMap
metadata:
  name: &amp;cmname myconfig
  namespace: &amp;namespace default
  labels:
    name: *cmname
    deployedInNamespace: *namespace
data:
  config.yaml: |
    [myconfig]
    example_field=1
</code></pre>
<p>This will result in:</p>
<pre><code>apiVersion: v1
data:
  config.yaml: |
    [myconfig]
    example_field=1
kind: ConfigMap
metadata:
  creationTimestamp: &quot;2023-01-25T10:06:27Z&quot;
  labels:
    deployedInNamespace: default
    name: myconfig
  name: myconfig
  namespace: default
  resourceVersion: &quot;147712&quot;
  uid: 4039cea4-1e64-4d1a-bdff-910d5ff2a485
</code></pre>
<p>As you can see the labels name &amp;&amp; deployedInNamespace have the values resulted from the anchor evaluation.</p>
<p>Based on your use case description, what you would need is going the Helm chart path and template your manifests. You can then leverage helper functions and easily customize when you want these fields. From my experience, when you have an use case like this, Helm is the way to go, because it will help you customize everything within your manifests when you decide to change something else.</p>
