如何在没有引号的情况下使用 k8s Ansible 模块? [英] How to use k8s Ansible module without quotes?
问题描述
我正在尝试使用模块 community.kubernetes.k8s – 使用角色变量(例如 role/sampleRole/vars 文件)管理 Kubernetes (K8s) 对象.
I am trying to use the module community.kubernetes.k8s – Manage Kubernetes (K8s) objects with variables from the role (e.g. role/sampleRole/vars file).
我在整数点上失败了,例如:
I am failing when it comes to the integer point e.g.:
- 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 }}
当我使用这种格式进行部署时,它显然会失败,因为它无法解析引用";到变量.
When I deploy with this format obviously it will fail at it can not parse the "reference" to the 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 }}"
然后我收到以下错误(部分):
When I use quotes on the variable e.g. - containerPort: "{{ containerPort }}"
then I get the following error (part of it):
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 | 将字符串转换为 intint }}"
但它没有用.问题似乎来自引号,独立于我如何在我的 var 文件中定义 var,例如containerPort:80
或 containerPort:80"
.
I tried to cast the string to int by using - containerPort: "{{ containerPort | int }}"
but it did not worked. The problem seems to be coming from the quotes, independently how I define the var in my var file e.g. containerPort: 80
or containerPort: "80"
.
我在论坛Ansible、k8s和变量上发现了一个类似的问题,但用户似乎不要有和我一样的问题.
I found a similar question on the forum Ansible, k8s and variables but the user seems not to have the same problems that I am having.
我正在运行最新版本的模块:
I am running with the latest version of the module:
$ 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
是否有解决此问题的方法或错误?
Is there any workaround this problem or is it a bug?
更新 (08-01-2020):该问题已在 0.17.0 版本中修复.
Update (08-01-2020): The problem is fixed on version 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
推荐答案
您可以尝试以下解决方案;在这个例子中,我们正在创建一个文本模板,然后使用 from_yaml
过滤器将其转换为我们想要的数据结构:
You could try the following as a workaround; in this example, we're creating a text template, and then using the from_yaml
filter to transform this into our desired data structure:
- name: sample
community.kubernetes.k8s:
state: present
definition:
apiVersion: apps/v1
kind: Deployment
metadata:
name: "{{ name }}"
namespace: "{{ namespace }}"
labels:
app: "{{ app }}"
spec: "{{ spec|from_yaml }}"
vars:
spec: |
replicas: 2
selector:
matchLabels:
app: "{{ app }}"
template:
metadata:
labels:
app: "{{ app }}"
spec:
containers:
- name: "{{ name }}"
image: "{{ image }}"
ports:
- containerPort: {{ containerPort }}
这篇关于如何在没有引号的情况下使用 k8s Ansible 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!