<p>I'm using Ansible and the <code>k8s</code> module for deploying applications to an OpenShift cluster. In general this is working really well.</p>

<p>However, when I try to set the port value, in a deployment config, using a value from a variable, things are not so happy.</p>

<p>I have the following ansible task as an example:</p>

<pre><code>- name: Create app service
      k8s:
        name: "{{ name | lower }}"
        state: present
        definition:
          apiVersion: v1
          kind: Service
          metadata:
            annotations:
            labels:
              app: "{{ name | lower }}"
            name: "{{ name | lower }}"
            namespace: "{{ name | lower }}"
          spec:
            ports:
              - name: "{{ port }}-tcp"
                port: "{{ port  }}"
                protocol: TCP
                targetPort: "{{ port | int }}" &lt;--- the problem!
            selector:
              deploymentconfig: "{{ name | lower }}"
            sessionAffinity: None
            type: ClusterIP
          status:
            loadBalancer: {}
</code></pre>

<p>The variable is set in a yaml file, which is read into the playbook, and the variable is set like <code>port: "5000"</code>.</p>

<p>If I change this to <code>port: 5000</code> then it solves the problem, but I use this variable in several other places and other playbooks, so I would prefer to keep the variable as is.</p>

<p>I have tried using the approaches to solve this:
<code>"{{ port | int }}"</code></p>

<p>An example of the error I get is:</p>

<pre><code>fatal: [localhost]: FAILED! =&gt; {"changed": false, "error": 422, "msg": "Failed to patch object: {\"kind\":\"Status\",\"apiVersion\":\"v1\",\"metadata\":{},\"status\":\"Failure\",\"message\":\"Service \\\"myapp\\\" is invalid: spec.ports[0].targetPort: Invalid value: \\\"7001\\\": must contain at least one letter or number (a-z, 0-9)\",\"reason\":\"Invalid\",\"details\":{\"name\":\"usdt-wallet\",\"kind\":\"Service\",\"causes\":[{\"reason\":\"FieldValueInvalid\",\"message\":\"Invalid value: \\\"5000\\\": must contain at least one letter or number (a-z, 0-9)\",\"field\":\"spec.ports[0].targetPort\"}]},\"code\":422}\n", "reason": "Unprocessable Entity", "status": 422}
</code></pre>

<p>According to the posted error message, your problem isn't <code>|int</code> or <code>|string</code> -- although I agree the error message is misleading:</p>

<blockquote>
  <p>"message": "Service \"usdt-wallet\" is invalid: spec.ports[0].targetPort: Invalid value: \"70001\": must contain at least one letter or number (a-z, 0-9)",</p>
</blockquote>

<p>but it is caused by trying to use 70001 as a target port but TCP ports must be in the range 1 to 65535 inclusive, as stated by <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.14/#serviceport-v1-core" rel="nofollow noreferrer">the fine manual</a></p>
