如何使用不带引号的k8s Ansible模块? [英] How to use k8s Ansible module without quotes?

查看:46
本文介绍了如何使用不带引号的k8s Ansible模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用模块 community.kubernetes.k8s –使用角色中的变量(例如role/sampleRole/vars文件)管理Kubernetes(K8s)对象.

关于整数点,例如:

- 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将字符串转换为int:"{{containerPort |int}}" ,但没有成功.问题似乎来自引号,独立于我如何在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,k8和变量中发现了类似的问题,但用户似乎不会遇到我遇到的同样的问题.

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?

更新(2020年1月8日):此问题已在版本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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆