<p>Im trying to create yaml for deployment with kubernetes. I am using a same script for different environment, which is separated with namespace. Now, I need to access the namespace name within the deployment yaml, such as </p>

<pre><code>"name":"$(namespace)"
</code></pre>

<p>in the yaml file. Is it possible to do so?</p>

<p><em>edit</em> sorry, I may have misunderstood your question: if you want access to the current namespace in which the Pod is running, you can inject that into its environment via an <a href="https://kubernetes.io/docs/api-reference/v1.8/#envvarsource-v1-core" rel="nofollow noreferrer"><code>env: valueFrom:</code></a> construct, described in greater detail <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">here</a>:</p>

<pre><code>env:
- name: MY_NAMESPACE
  valueFrom:
    fieldRef:
        fieldPath: metadata.namespace
</code></pre>

<hr>

<p>Omit the <code>namespace:</code> from the yaml and provide it to <code>kubectl</code> as <code>kubectl --namespace=foo1 create -f my-thing.yaml</code> (assuming, of course, you're using <code>kubectl</code>; the idea behind the answer is the same, just the mechanics will change if using a different method)</p>

<p>You can also specify the default namespace in <code>~/.kube/config</code> in the context, and address it that way: <code>kubectl --context=server-foo1</code> which allows associating different credentials with the different namespaces, too. They all boil down to the same effect in the end, it's just a matter of which is the most convenient for your case.</p>

<p>The most extreme(?) form is that you can also have multiple configs and switch between them via <code>env KUBECONFIG=$TMPDIR/foo1.yaml kubectl create -f my-thing.yaml</code></p>
