<p>I am trying to get the id in the pod name as a environment variable in the yaml file:</p>
<pre><code>job-05d93d91-d527-4b06-ad89-593f0350ecd6-jobmanager--1-qfjqf      1/1     Running   0          22m
job-05d93d91-d527-4b06-ad89-593f0350ecd6-taskmanager-66758j5h4d   1/1     Running   0          22m
job-05d93d91-d527-4b06-ad89-593f0350ecd6-taskmanager-66758vsj5q   1/1     Running   0          22m
</code></pre>
<p>I want to have &quot;66758vsj5q&quot; and &quot;66758j5h4d&quot; and &quot;qfjqf&quot; as an env variable.</p>
<p>I am able to get the pod name by exposing:</p>
<pre><code> envVars:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
</code></pre>
<p>but POD_NAME from <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">Expose Pod Information to Containers Through Environment Variables</a> returns the whole pod name. I am actually using ververica platform and following <a href="https://docs.ververica.com/user_guide/application_operations/deployments/kubernetes.html#environment-variables" rel="nofollow noreferrer">this documentation</a>.</p>
<p>Is it possible to be done?</p>

Monika X
<p>Below are the supported things that you could get with <code>valueFrom</code>.</p>
<blockquote>
<p>valueFrom: Source for the environment variable's value. Cannot be used if value is not empty.</p>
<p>EnvVarSource represents a source for the value of an EnvVar.</p>
<p><strong>configMapKeyRef</strong> (object)</p>
<p>Selects a key of a ConfigMap.</p>
<p><strong>fieldRef</strong> (object)</p>
<p>Selects a field of the pod: supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP.</p>
<p><strong>resourceFieldRef</strong> (object)</p>
<p>Selects a resource of the container: only resources limits and requests (limits.cpu, limits.memory, limits.ephemeral-storage, requests.cpu, requests.memory and requests.ephemeral-storage) are currently supported.</p>
<p><strong>secretKeyRef</strong> (object)</p>
<p>Selects a key of a secret in the pod's namespace</p>
</blockquote>
<p>So the way you are trying, it does not work.</p>
<p>The string you are getting from <code>metadata.name</code> is actually already exposed as environment variable with name <code>HOSTNAME</code>.</p>
<p>Knowing the pattern of those names, <em>name-hash</em> you can use shell, or any other tool to get the last part. For example, by parameter expansion or by splitting on the hyphen. It would depend on which context you want to use that value.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
metadata:
  name: get-hash
spec:
  ttlSecondsAfterFinished: 100
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: get-hash
          image: busybox
          command: [&quot;sh&quot;, &quot;-c&quot;]
          args:
            - |
              echo &quot;the podname is: $HOSTNAME&quot;;
              echo &quot;the hash is: ${HOSTNAME##*-}&quot;
</code></pre>
<pre class="lang-sh prettyprint-override"><code>$ kubectl create -f get-hash.yaml
$ kubectl logs job.batch/get-hash
the podname is: get-hash-mpf7p
the hash is: mpf7p
</code></pre>
