Skip to content

ryandgoulding/helm-ansible-template-exporter

 
 

Repository files navigation

Helm Ansible Template Exporter

Description

This tool automates conversion from Helm Chart to Ansible Playbook Role. Helm Charts are defined utilizing Go templates, while Ansible Playbook Roles are defined using Yaml and Jinja2 templates. Due to the fact that Go and Jinja2 are completely separate languages, some facets of Helm charts are difficult or impossible to convert directly to Ansible Playbook roles. This tool attempts to automatically convert a Helm chart into an Ansible Playbook Role, but some aspects of the conversion process must be performed manually by hand after utilizing this tool.

Current Capabilities

The current offering does the following:

  1. Creates a role in the workspace directory using ansible-galaxy.
  2. Raw copies templates into the generated Ansible Playbook Role templates directory, renaming each template with a ".j2" extension.
  3. Merges values.yml (or values.yaml) into the generated Ansible Playbook Role defaults/main.yml file.
  4. Searches the generated Ansible Playbook Role's defaults/main.yml file for self references (i.e., references to .Values.) and comments them out. Ansible Playbook is incapable of expressing self references in defaults/main.yml, a clear technology gap between Ansible Playbook and Helm charts. A "WARN" message is output indicating that a manual change is required to defaults/main.yml on the appropriate lines.
  5. Convert Branch syntax for "if", "for" and "for key/value" in each template to utilize proper Jinja2 syntax. This includes a heuristic which attempts to determine if conditionals are checking for definition v.s. boolean evaluation.
  6. Convert boolean composition ordering. Go Templating utilizes "and " format. On the other hand, Jinja2 utilizes " and " formatting. helmExport handles this conversion automatically.
  7. Template functions invocations are converted to Jinja2 Ansible Filter invocations. This requires converting direct function invocations to piped invocations, as well as inserting the appropriate parentheses and commas for argument lists.
  8. Removes references to ".Values." in the generated Ansible Playbook's Roles' templates. Ansible Playbook can directly reference the values in defaults/main.yml, so this reference isn't needed.
  9. If the "generateFilters" flag is set to true, some stub Ansible Filter implementations are installed for use in converting Go template functions to Ansible Playbook Filters.

Known Limitations

Go/Sprig Template Function Limitations

Ansible Playbook Filter(s) are one candidate to replace Go/Sprig Template Functions. However, they are not a direct 1:1 mapping, so a few extra steps must be taken after conversion.

Implement or Replace Template Function Invocations with Ansible Filters

There are a number of Ansible Playbook Filters built directly into Ansible. For instances where there does not seem to be a replacement, you will need to create your own replacement. If you utilize the "generateFilters" command line argument, some example filters will be installed into your generated Ansible Playbook Role.

"template" and "include" are not supported

Go Templates provide "template" and "include" in order to support dynamic template creation. Ansible has no direct replacement, and although there are some similar constructs, helmExport currently doesn't support any conversion. Instead, consider using Ansible defaults as a replacement.

Building

To build the code, use the following command:

make

To clean the code base, use the following command:

make clean

Running the Code

Runtime Dependencies

Helm Ansible Template Exporter requires ansible-galaxy to initialize exported roles. Additionally, ansible-galaxy must be included in the $PATH. Additionally, if you utilize the "generateFilters" flag, then a Go compiler must be installed.

Run Instructions

make run

This will run the current implementation of code.

Alternatively:

./helmExport export nginx --helm-chart=./example --workspace=./workspace --generateFilters=true

About

This is a tool to convert Helm charts into Ansible Role

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 91.0%
  • Python 5.3%
  • Shell 3.4%
  • Makefile 0.3%