Ansible,k8s和变量 [英] Ansible, k8s and variables
问题描述
我正在使用Ansible和k8s
模块将应用程序部署到OpenShift集群.总的来说,这真的很好.
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: {}
将变量设置在yaml文件中,该文件将被读取到剧本中,并且变量的设置类似于port: "5000"
.
The variable is set in a yaml file, which is read into the playbook, and the variable is set like port: "5000"
.
如果我将其更改为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.
我尝试使用以下方法来解决此问题:
"{{ port | int }}"
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}
推荐答案
根据发布的错误消息,您的问题不是|int
或|string
-尽管我同意错误消息具有误导性:
According to the posted error message, your problem isn't |int
or |string
-- although I agree the error message is misleading:
消息":服务\"usdt-wallet \"无效:spec.ports [0] .targetPort:无效值:\"70001 \":必须至少包含一个字母或数字(az,0- 9)",
"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)",
,但这是由于尝试将70001用作目标端口而引起的,但是TCP端口必须在1到65535(包括1和65535)之间,如
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
这篇关于Ansible,k8s和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!