How to use k8s Ansible module without quotes?

12/22/2020

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.

Sample of error:

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

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}

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".

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?

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
-- Thanos
ansible
kubernetes

2 Answers

12/22/2020

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 }}
-- larsks
Source: StackOverflow

12/28/2020

The solution provided by larsks works perfectly. Although I got another problem on my case where I use templates with a bit more complex cases (e.g. loops etc) where I found my self having the same problem.

The only solution that I had before was to use ansible.builtin.template – Template a file out to a remote server and simply ssh the some_file.yml.j2 to one of my Master nodes and deploy through ansible.builtin.shell – Execute shell commands on targets (e.g. kubectl apply -f some_file.yml).

Thanks to community.kubernetes.k8s – Manage Kubernetes (K8s) objects I am able to do all this work with a single task e.g. (example taken from documentation):

- name: Read definition template file from the Ansible controller file system
  community.kubernetes.k8s:
    state: present
    template: '/testing/deployment.j2'

The only requirement that the user needs to have in advance is to have the kubeconfig file placed in the default location (~/.kube/config) or use the kubeconfig flag to point to the location of the file.

As a last step I use it delegate_to to localhost command e.g.

- name: Read definition template file from the Ansible controller file system
  community.kubernetes.k8s:
    state: present
    template: '/testing/deployment.j2'
  delegate_to: localhost

The way that this task works is that the user ssh to himself and run kubectl apply -f some_file.yml.j2 towards the LB or Master node API and the API applies the request (if the user has the permissions).

-- Thanos
Source: StackOverflow