问题
I'm using Ansible and the k8s
module for deploying applications to an OpenShift cluster. In general this is working really well.
However, when I try to set the port value, in a deployment config, using a value from a variable, things are not so happy.
I have the following ansible task as an example:
- 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 }}" <--- the problem!
selector:
deploymentconfig: "{{ name | lower }}"
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
The variable is set in a yaml file, which is read into the playbook, and the variable is set like port: "5000"
.
If I change this to port: 5000
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.
I have tried using the approaches to solve this:
"{{ port | int }}"
An example of the error I get is:
fatal: [localhost]: FAILED! => {"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}
回答1:
According to the posted error message, your problem isn't |int
or |string
-- although I agree the error message is misleading:
"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)",
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 the fine manual
来源:https://stackoverflow.com/questions/55821144/ansible-k8s-and-variables