首页 > 解决方案 > 如何使用不带引号的 k8s Ansible 模块?

问题描述

我正在尝试使用模块community.kubernetes.k8s –使用来自角色的变量(例如角色/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 }}

当我使用这种格式进行部署时,它显然会失败,因为它无法解析对 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}

我试图通过使用将字符串转换为 int ,- containerPort: "{{ containerPort | int }}"但它没有奏效。问题似乎来自引号,独立于我如何在我的 var 文件中定义 varcontainerPort: 80containerPort: "80".

我在论坛Ansible、k8s 和变量上发现了一个类似的问题,但用户似乎没有遇到与我相同的问题。

我正在使用最新版本的模块运行:

$ 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

标签: kubernetesansible

解决方案


您可以尝试以下解决方法;在本例中,我们创建了一个文本模板,然后使用from_yaml过滤器将其转换为我们想要的数据结构:

- 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 }}

推荐阅读