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.
The current offering does the following:
- Creates a role in the workspace directory using ansible-galaxy.
- Raw copies templates into the generated Ansible Playbook Role templates directory, renaming each template with a ".j2" extension.
- Merges values.yml (or values.yaml) into the generated Ansible Playbook Role defaults/main.yml file.
- 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.
- 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.
- Convert boolean composition ordering. Go Templating utilizes "and " format. On the other hand, Jinja2 utilizes " and " formatting. helmExport handles this conversion automatically.
- 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.
- 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.
- 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.
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.
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.
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.
To build the code, use the following command:
make
To clean the code base, use the following command:
make clean
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.
make run
This will run the current implementation of code.
Alternatively:
./helmExport export nginx --helm-chart=./example --workspace=./workspace --generateFilters=true