Ansible、k8s 和变量 [英] Ansible, k8s and variables
问题描述
我使用 Ansible 和 k8s
模块将应用程序部署到 OpenShift 集群.总的来说,这非常有效.
然而,当我尝试在部署配置中设置端口值时,使用来自变量的值,事情并不那么愉快.
我以以下 ansible 任务为例:
- 名称:创建应用服务k8s:名称:{{ 名称 | 下}}"状态:现在定义:api版本:v1种类:服务元数据:注释:标签:应用程序:{{ 名称 | 下}}"名称:{{ 名称 | 下}}"命名空间:{{ 名称 | 下}}"规格:端口:- 名称:{{ 端口 }}-tcp"端口:{{端口}}"协议:TCPtargetPort: "{{ port | int }}" <--- 问题!选择器:部署配置:{{名称|较低}}"sessionAffinity:无类型:集群IP地位:负载均衡器:{}
变量设置在yaml文件中,读入playbook,变量设置如port:"5000"
.
如果我将其更改为 port: 5000
那么它就解决了问题,但我在其他几个地方和其他剧本中使用了这个变量,所以我更愿意保持这个变量不变.>
我已尝试使用以下方法来解决此问题:"{{ port | int }}"
我得到的错误示例是:
fatal: [localhost]: 失败!=>{"changed": false, "error": 422, "msg": "无法修补对象:{\"kind\":\"Status\",\"apiVersion\":\"v1\",\"metadata\":{},\"status\":\"Failure\",\"message\":\"Service\\\"myapp\\\" 无效:spec.ports[0].targetPort: Invalidvalue: \\\"7001\\\": 必须至少包含一个字母或数字(az, 0-9)\",\"reason\":\"Invalid\",\"details\":{\"name\":\"usdt-wallet\",\"kind\":\"Service\",\"causes\":[{\"reason\":\"FieldValueInvalid\",\"message\":\"无效值:\\\"5000\\\": 必须至少包含一个字母或数字(az, 0-9)\",\"field\":\"spec.ports[0].targetPort\"}]},\"code\":422}\n", "reason": "Unprocessable Entity", "status": 422}
根据贴出的错误信息,你的问题不是 |int
或 |string
--虽然我同意错误消息具有误导性:
"message": "Service \"usdt-wallet\" is invalid: spec.ports[0].targetPort: Invalid value: \"70001\": 必须至少包含一个字母或数字(az, 0-9)",
但它是由于尝试使用 70001 作为目标端口引起的,但 TCP 端口必须在 1 到 65535 的范围内(包括 1 到 65535,如 精美手册
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}
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
这篇关于Ansible、k8s 和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!