<p>I am trying to use the module <a href="https://docs.ansible.com/ansible/latest/collections/community/kubernetes/k8s_module.html" rel="nofollow noreferrer">community.kubernetes.k8s – Manage Kubernetes (K8s) objects</a> with variables from the role (e.g. role/sampleRole/vars file).</p>
<p>I am failing when it comes to the integer point e.g.:</p>
<pre class="lang-yaml prettyprint-override"><code>- name: sample                                                                                                                                                         
  community.kubernetes.k8s:                                                                                                                                                                                                                      
  state: present                                                                                                                                                                                                                               
  definition:                                                                                                                                                                                                                                    
    apiVersion: apps/v1                                                                                                                                                                                                                          
    kind: Deployment                                                                                                                                                                                                                             
    metadata:                                                                                                                                                                                                                                      
      name: &quot;{{ name }}&quot;
      namespace: &quot;{{ namespace }}&quot;                                                                                                                                                                                             
      labels:
        app: &quot;{{ app }}&quot;                                                                                                                                                                                              
    spec:
      replicas: 2
        selector:
          matchLabels:
            app: &quot;{{ app }}&quot;                                                                                                                                                                                    
        template:
          metadata:
            labels:
              app: &quot;{{ app }}&quot;                                                                                                                                                                                
          spec:
            containers:
            - name: &quot;{{ name }}&quot;                                                                                                                                                                                  
              image: &quot;{{ image }}&quot;                                                                                                                                                                                
              ports:
              - containerPort: {{ containerPort }}
</code></pre>
<p>When I deploy with this format obviously it will fail at it can not parse the &quot;reference&quot; to the var.</p>
<p>Sample of error:</p>
<pre class="lang-sh prettyprint-override"><code>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 &lt;some line&gt;, column &lt;some column&gt;, 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:
      - &quot;{{ foo }}&quot;
</code></pre>
<p>When I use quotes on the variable e.g. <code>- containerPort: &quot;{{ containerPort }}&quot;</code> then I get the following error (part of it):</p>
<pre class="lang-sh prettyprint-override"><code>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\\\\\&quot;:\\\\\&quot;80\\\\\&quot;}]}],\\\\\&quot;d|..., bigger context ...|\\\\\&quot;,\\\\\&quot;name\\\\\&quot;:\\\\\&quot;samplegreen\\\\\&quot;,\\\\\&quot;ports\\\\\&quot;:[{\\\\\&quot;containerPort\\\\\&quot;:\\\\\&quot;80\\\\\&quot;}]}],\\\\\&quot;dnsPolicy\\\\\&quot;:\\\\\&quot;ClusterFirst\\\\\&quot;,\\\\\&quot;restartPolicy\\\\\&quot;|...\&quot;,\&quot;field\&quot;:\&quot;patch\&quot;}]},\&quot;code\&quot;:422}\\n'&quot;, &quot;reason&quot;: &quot;Unprocessable Entity&quot;, &quot;status&quot;: 422}
</code></pre>
<p>I tried to cast the string to int by using <code>- containerPort: &quot;{{ containerPort | int }}&quot;</code> 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. <code>containerPort: 80</code> or <code>containerPort: &quot;80&quot;</code>.</p>
<p>I found a similar question on the forum <a href="https://stackoverflow.com/questions/55821144/ansible-k8s-and-variables">Ansible, k8s and variables</a> but the user seems not to have the same problems that I am having.</p>
<p>I am running with the latest version of the module:</p>
<pre class="lang-sh prettyprint-override"><code>$ 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
</code></pre>
<p>Is there any workaround this problem or is it a bug?</p>
<p><strong>Update (08-01-2020):</strong> The problem is fixed on version 0.17.0.</p>
<pre class="lang-sh prettyprint-override"><code>$ 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
</code></pre>

Thanos
<p>You could try the following as a workaround; in this example, we're creating a text template, and then using the <code>from_yaml</code> filter to transform this into our desired data structure:</p>
<pre><code>- name: sample                                                                                                                                                         
  community.kubernetes.k8s:                                                                                                                                                                                                                      
    state: present                                                                                                                                                                                                                               
    definition:                                                                                                                                                                                                                                    
      apiVersion: apps/v1                                                                                                                                                                                                                          
      kind: Deployment                                                                                                                                                                                                                             
      metadata:                                                                                                                                                                                                                                      
        name: &quot;{{ name }}&quot;
        namespace: &quot;{{ namespace }}&quot;                                                                                                                                                                                             
        labels:
          app: &quot;{{ app }}&quot;                                                                                                                                                                                              
      spec: &quot;{{ spec|from_yaml }}&quot;
  vars:
    spec: |
      replicas: 2
        selector:
          matchLabels:
            app: &quot;{{ app }}&quot;                                                                                                                                                                                    
        template:
          metadata:
            labels:
              app: &quot;{{ app }}&quot;                                                                                                                                                                                
          spec:
            containers:
            - name: &quot;{{ name }}&quot;                                                                                                                                                                                  
              image: &quot;{{ image }}&quot;                                                                                                                                                                                
              ports:
              - containerPort: {{ containerPort }}
</code></pre>
