【发布时间】:2021-08-04 21:32:35
【问题描述】:
我正在尝试将模块 community.kubernetes.k8s – Manage Kubernetes (K8s) objects 与来自角色的变量(例如角色/sampleRole/vars 文件)一起使用。
当谈到整数点时我失败了,例如:
- name: sample
community.kubernetes.k8s:
state: present
definition:
apiVersion: apps/v1
kind: Deployment
metadata:
name: "{{ name }}"
namespace: "{{ namespace }}"
labels:
app: "{{ app }}"
spec:
replicas: 2
selector:
matchLabels:
app: "{{ app }}"
template:
metadata:
labels:
app: "{{ app }}"
spec:
containers:
- name: "{{ name }}"
image: "{{ image }}"
ports:
- containerPort: {{ containerPort }}
当我使用这种格式部署时,它显然会失败,因为它无法解析对 var 的“引用”。
错误示例:
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)
Syntax Error while loading YAML.
found unacceptable key (unhashable type: 'AnsibleMapping')
The error appears to be in 'deploy.yml': line <some line>, column <some column>, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
ports:
- containerPort: {{ containerPort }}
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
当我在变量上使用引号时,例如- containerPort: "{{ containerPort }}" 然后我收到以下错误(部分错误):
v1.Deployment.Spec: v1.DeploymentSpec.Template: v1.PodTemplateSpec.Spec: v1.PodSpec.Containers: []v1.Container: v1.Container.Ports: []v1.ContainerPort: v1.ContainerPort.ContainerPort: readUint32: unexpected character: \\\\ufffd, error found in #10 byte of ...|nerPort\\\\\":\\\\\"80\\\\\"}]}],\\\\\"d|..., bigger context ...|\\\\\",\\\\\"name\\\\\":\\\\\"samplegreen\\\\\",\\\\\"ports\\\\\":[{\\\\\"containerPort\\\\\":\\\\\"80\\\\\"}]}],\\\\\"dnsPolicy\\\\\":\\\\\"ClusterFirst\\\\\",\\\\\"restartPolicy\\\\\"|...\",\"field\":\"patch\"}]},\"code\":422}\\n'", "reason": "Unprocessable Entity", "status": 422}
我尝试使用- containerPort: "{{ containerPort | int }}" 将字符串转换为int,但没有成功。问题似乎来自引号,独立于我如何在我的 var 文件中定义 var,例如containerPort: 80 或 containerPort: "80"。
我在论坛Ansible, k8s and variables 上发现了一个类似的问题,但用户似乎没有遇到与我相同的问题。
我正在使用最新版本的模块:
$ python3 -m pip show openshift
Name: openshift
Version: 0.11.2
Summary: OpenShift python client
Home-page: https://github.com/openshift/openshift-restclient-python
Author: OpenShift
Author-email: UNKNOWN
License: Apache License Version 2.0
Location: /usr/local/lib/python3.8/dist-packages
Requires: ruamel.yaml, python-string-utils, jinja2, six, kubernetes
是否有任何解决方法或者它是一个错误?
更新(08-01-2020):该问题已在 0.17.0 版本中得到修复。
$ python3 -m pip show k8s
Name: k8s
Version: 0.17.0
Summary: Python client library for the Kubernetes API
Home-page: https://github.com/fiaas/k8s
Author: FiaaS developers
Author-email: fiaas@googlegroups.com
License: Apache License
Location: /usr/local/lib/python3.8/dist-packages
Requires: requests, pyrfc3339, six, cachetools
【问题讨论】:
-
你能告诉我们你是如何设置
containerPort的吗? -
@larsks 不管我将它定义为
containerPort: "80"还是containerPort: 80仍然是一样的错误。 -
You must quote the Jinja template 以免你得到这个确切的 YAML 错误。
containerPort: "{{ containerPort }}"是否有效,即使它看起来像错误的类型?还是"{{ containerPort | int }}"? -
@DavidMaze 我已经尝试过包括演员表和不演员表。错误来自引号。我需要找到一种方法来不使用 larsks 提议的引号。他的示例运行良好,但这是一种解决方法。我认为这是一个错误。我会在github上提出。
标签: kubernetes ansible