1 year ago

#373042

test-img

MFranc

Use ansible template for Helm's value files with multiline quoted strings

I'm trying to write an ansible role to install helm-charts.
The role structure, simplified for posting, is something like this:

my_role
└───defaults
    └───main.yml
└───tasks
    └───main.yml
└───templates
    └───main.yml

The role's default value file includes a variable chart_values used to pass the chart's values. It is used only in the template/main.yml file, inside it there is simply the variable expansion, like this:

"{{ chart_values }}"

In tasks/main.yml the template is used as follows:

- name: Deploy chart.
  kubernetes.core.helm:
    release_name: "{{ chart_release_name }}"
    release_namespace: "{{ chart_release_namespace }}"
    release_state: present
    chart_ref: "{{ chart_chart_ref }}"
    chart_version: "{{ chart_chart_version }}"
    update_repo_cache: true
    release_values: "{{ lookup('template', 'chart_values.yml') | from_yaml }}"

Values seem to be rendered correctly, except when there is a multiline object containing quotes. For instance, while deploying gitlab runners, a toml configuration snippet is required. When I try to run the playbook with

chart_values:
  replicas: 5
  runners:
    config: |
      [[runners]]
        [runners.kubernetes]
          namespace = "gitlab-runners"
          image = "ubuntu:20.04"

Ansible returns the following error:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was:   in "<unicode string>", line 2, column 770
fatal: [127.0.0.1]: FAILED! => {"changed": false}

The more verbose output is:

The full traceback is:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/ansible/executor/task_executor.py", line 504, in _execute
    self._task.post_validate(templar=templar)
  File "/usr/lib/python3/dist-packages/ansible/playbook/task.py", line 285, in post_validate
    super(Task, self).post_validate(templar)
  File "/usr/lib/python3/dist-packages/ansible/playbook/base.py", line 650, in post_validate
    value = templar.template(getattr(self, name))
  File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 913, in template
    d[k] = self.template(
  File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 869, in template
    result = self.do_template(
  File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 1139, in do_template
    res = j2_concat(rf)
  File "<template>", line 12, in root
  File "/usr/lib/python3/dist-packages/ansible/template/__init__.py", line 298, in wrapper
    ret = func(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/ansible/plugins/filter/core.py", line 207, in from_yaml
    return yaml_load(text_type(to_text(data, errors='surrogate_or_strict')))
  File "/usr/local/lib/python3.8/dist-packages/yaml/__init__.py", line 81, in load
    return loader.get_single_data()
  File "/usr/local/lib/python3.8/dist-packages/yaml/constructor.py", line 49, in get_single_data
    node = self.get_single_node()
  File "yaml/_yaml.pyx", line 674, in yaml._yaml.CParser.get_single_node
  File "yaml/_yaml.pyx", line 860, in yaml._yaml.CParser._parse_next_event
yaml.parser.ParserError: did not find expected <document start>
  in "<unicode string>", line 2, column 770
fatal: [127.0.0.1]: FAILED! => {
    "changed": false
}

ansible

jinja2

kubernetes-helm

helm3

0 Answers

Your Answer

Accepted video resources