ansiblekubernetes-helmansible-template

Error using ansible jinja2 template with helm values


I am using ansible jinja2 template to create values for a helm chart. I am getting an error template error while templating string: unexpected 'end of statement block'. This happens after helm uses the values file. This is a sample format not the original.

The ansible vars file:

apartment:
  size: "2000"
  floor: "10"
  numbers:
    - 1
    - 2
    - 3

Ansible j2 template:

floorconfig:
    enabled: true
    aptnumbers: {% for item in apartment.numbers +%}
        - {{ item }}
          {%- endfor %}

Desired output:

floorconfig:
    enabled: true
    aptnumbers: 
        - 1
        - 2
        - 3 

Task :

- name: Deploy version of helm chart
  local_action:
    module: kubernetes.core.helm
    host: "https://{{ inventory_hostname }}:6443"
    kubeconfig: "/etc/ansible/{{ inventory_hostname }}/etc/kubernetes/admin.conf"
    name: kube-apt
    chart_ref: apts/apt-stack
    chart_version: 26.0.0
    wait: True
    update_repo_cache: True
    wait_timeout: "10m"
    state: present
    values: "{{ lookup('template', 'templates/my_Values.yml.j2') | from_yaml }}"

Output Error:

FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'template'. Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: unexpected 'end of statement block'.

Solution

  • You are making this overly complicated IMO.

    1. you are looping over a list to recreate the exact same elements in what appears to be a yaml template. Why not using the list directly ?
    2. you are going through a template to load back yaml values where you can just define the needed var.

    You could just drop the template completely and simplify your value declaration in your task like:

    - name: Deploy version of helm chart
      kubernetes.core.helm:
        host: "https://{{ inventory_hostname }}:6443"
        kubeconfig: "/etc/ansible/{{ inventory_hostname }}/etc/kubernetes/admin.conf"
        name: kube-apt
        chart_ref: apts/apt-stack
        chart_version: 26.0.0
        wait: True
        update_repo_cache: True
        wait_timeout: "10m"
        state: present
        values:
          floorconfig:
            enabled: true
            aptnumbers: "{{ apartment.numbers }}"
      delegate_to: localhost
    

    If you really want to go through a separate file, just make it an other var file, define the entire dictionary there, load it and use the dictionary in the value above.