mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-07-29 10:54:41 +00:00
Add support for helm template (#368)
* Add support for helm template This commit adds basic support for helm template. The functionality allows one to either render a chart's templates to a specific directory, or capture the rendered templates as a string of concatenated yaml documents. * Add changelog fragment * Address PR feedback * Set changed to true
This commit is contained in:
2
changelogs/fragments/368-helm_template.yaml
Normal file
2
changelogs/fragments/368-helm_template.yaml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
minor_changes:
|
||||||
|
- helm_template - add helm_template module to support template functionality (https://github.com/ansible-collections/community.kubernetes/issues/367).
|
||||||
@@ -1,321 +1,365 @@
|
|||||||
---
|
---
|
||||||
- name: Check helm_info empty
|
- name: Chart tests
|
||||||
helm_info:
|
block:
|
||||||
binary_path: "{{ helm_binary }}"
|
- name: Create temp directory
|
||||||
name: test
|
tempfile:
|
||||||
namespace: "{{ helm_namespace }}"
|
state: directory
|
||||||
register: empty_info
|
register: tmpdir
|
||||||
|
|
||||||
- name: "Assert that no charts are installed with helm_info"
|
- name: Set temp directory fact
|
||||||
assert:
|
set_fact:
|
||||||
that:
|
temp_dir: "{{ tmpdir.path }}"
|
||||||
- empty_info.status is undefined
|
|
||||||
|
|
||||||
- name: "Install fail {{ chart_test }} from {{ source }}"
|
- name: Check helm_info empty
|
||||||
helm:
|
helm_info:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
chart_ref: "{{ chart_source }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
register: empty_info
|
||||||
namespace: "{{ helm_namespace }}"
|
|
||||||
ignore_errors: yes
|
|
||||||
register: install_fail
|
|
||||||
|
|
||||||
- name: "Assert that Install fail {{ chart_test }} from {{ source }}"
|
- name: "Assert that no charts are installed with helm_info"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install_fail is failed
|
- empty_info.status is undefined
|
||||||
- "'Error: create: failed to create: namespaces \"' + helm_namespace + '\" not found' in install_fail.stderr"
|
|
||||||
|
|
||||||
- name: "Install {{ chart_test }} from {{ source }} in check mode"
|
- name: "Install fail {{ chart_test }} from {{ source }}"
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
chart_ref: "{{ chart_source }}"
|
chart_ref: "{{ chart_source }}"
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
create_namespace: true
|
ignore_errors: yes
|
||||||
register: install_check_mode
|
register: install_fail
|
||||||
check_mode: true
|
|
||||||
|
|
||||||
- name: "Assert that {{ chart_test }} chart is installed from {{ source }} in check mode"
|
- name: "Assert that Install fail {{ chart_test }} from {{ source }}"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install_check_mode is changed
|
- install_fail is failed
|
||||||
- install_check_mode.status is defined
|
- "'Error: create: failed to create: namespaces \"' + helm_namespace + '\" not found' in install_fail.stderr"
|
||||||
- install_check_mode.status.values is defined
|
|
||||||
|
|
||||||
- name: "Install {{ chart_test }} from {{ source }}"
|
- name: "Install {{ chart_test }} from {{ source }} in check mode"
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
chart_ref: "{{ chart_source }}"
|
chart_ref: "{{ chart_source }}"
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
create_namespace: true
|
create_namespace: true
|
||||||
register: install
|
register: install_check_mode
|
||||||
|
check_mode: true
|
||||||
|
|
||||||
- name: "Assert that {{ chart_test }} chart is installed from {{ source }}"
|
- name: "Assert that {{ chart_test }} chart is installed from {{ source }} in check mode"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is changed
|
- install_check_mode is changed
|
||||||
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
- install_check_mode.status is defined
|
||||||
- install.status.status | lower == 'deployed'
|
- install_check_mode.status.values is defined
|
||||||
|
|
||||||
- name: Check helm_info content
|
- name: "Install {{ chart_test }} from {{ source }}"
|
||||||
helm_info:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
namespace: "{{ helm_namespace }}"
|
chart_ref: "{{ chart_source }}"
|
||||||
register: content_info
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
|
namespace: "{{ helm_namespace }}"
|
||||||
|
create_namespace: true
|
||||||
|
register: install
|
||||||
|
|
||||||
- name: "Assert that {{ chart_test }} is installed from {{ source }} with helm_info"
|
- name: "Assert that {{ chart_test }} chart is installed from {{ source }}"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- content_info.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
- install is changed
|
||||||
- content_info.status.status | lower == 'deployed'
|
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
||||||
|
- install.status.status | lower == 'deployed'
|
||||||
|
|
||||||
- name: Check idempotency
|
- name: Check helm_info content
|
||||||
helm:
|
helm_info:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
chart_ref: "{{ chart_source }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
register: content_info
|
||||||
namespace: "{{ helm_namespace }}"
|
|
||||||
register: install
|
|
||||||
|
|
||||||
- name: Assert idempotency
|
- name: "Assert that {{ chart_test }} is installed from {{ source }} with helm_info"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is not changed
|
- content_info.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
||||||
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
- content_info.status.status | lower == 'deployed'
|
||||||
- install.status.status | lower == 'deployed'
|
|
||||||
|
|
||||||
- name: "Add vars to {{ chart_test }} from {{ source }}"
|
- name: Check idempotency
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
chart_ref: "{{ chart_source }}"
|
chart_ref: "{{ chart_source }}"
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
values: "{{ chart_test_values }}"
|
register: install
|
||||||
register: install
|
|
||||||
|
|
||||||
- name: "Assert that {{ chart_test }} chart is upgraded with new var from {{ source }}"
|
- name: Assert idempotency
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is changed
|
- install is not changed
|
||||||
- install.status.status | lower == 'deployed'
|
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
||||||
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
- install.status.status | lower == 'deployed'
|
||||||
- "install.status['values'].revisionHistoryLimit == 0"
|
|
||||||
|
|
||||||
- name: Check idempotency after adding vars
|
- name: "Add vars to {{ chart_test }} from {{ source }}"
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
chart_ref: "{{ chart_source }}"
|
chart_ref: "{{ chart_source }}"
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
values: "{{ chart_test_values }}"
|
values: "{{ chart_test_values }}"
|
||||||
register: install
|
register: install
|
||||||
|
|
||||||
- name: Assert idempotency after add vars
|
- name: "Assert that {{ chart_test }} chart is upgraded with new var from {{ source }}"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is not changed
|
- install is changed
|
||||||
- install.status.status | lower == 'deployed'
|
- install.status.status | lower == 'deployed'
|
||||||
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
||||||
- "install.status['values'].revisionHistoryLimit == 0"
|
- "install.status['values'].revisionHistoryLimit == 0"
|
||||||
|
|
||||||
- name: "Remove Vars to {{ chart_test }} from {{ source }}"
|
- name: Check idempotency after adding vars
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
chart_ref: "{{ chart_source }}"
|
chart_ref: "{{ chart_source }}"
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
register: install
|
values: "{{ chart_test_values }}"
|
||||||
|
register: install
|
||||||
|
|
||||||
- name: "Assert that {{ chart_test }} chart is upgraded with new var from {{ source }}"
|
- name: Assert idempotency after add vars
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is changed
|
- install is not changed
|
||||||
- install.status.status | lower == 'deployed'
|
- install.status.status | lower == 'deployed'
|
||||||
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
||||||
- install.status['values'] == {}
|
- "install.status['values'].revisionHistoryLimit == 0"
|
||||||
|
|
||||||
- name: Check idempotency after removing vars
|
- name: "Remove Vars to {{ chart_test }} from {{ source }}"
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
chart_ref: "{{ chart_source }}"
|
chart_ref: "{{ chart_source }}"
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
register: install
|
register: install
|
||||||
|
|
||||||
- name: Assert idempotency after removing vars
|
- name: "Assert that {{ chart_test }} chart is upgraded with new var from {{ source }}"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is not changed
|
- install is changed
|
||||||
- install.status.status | lower == 'deployed'
|
- install.status.status | lower == 'deployed'
|
||||||
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
||||||
- install.status['values'] == {}
|
- install.status['values'] == {}
|
||||||
|
|
||||||
- name: "Upgrade {{ chart_test }} from {{ source }}"
|
- name: Check idempotency after removing vars
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
chart_ref: "{{ chart_source_upgrade | default(chart_source) }}"
|
chart_ref: "{{ chart_source }}"
|
||||||
chart_version: "{{ chart_source_version_upgrade | default(omit) }}"
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
register: install
|
register: install
|
||||||
|
|
||||||
- name: "Assert that {{ chart_test }} chart is upgraded with new version from {{ source }}"
|
- name: Assert idempotency after removing vars
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is changed
|
- install is not changed
|
||||||
- install.status.status | lower == 'deployed'
|
- install.status.status | lower == 'deployed'
|
||||||
- install.status.chart == "{{ chart_test }}-{{ chart_test_version_upgrade }}"
|
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
||||||
|
- install.status['values'] == {}
|
||||||
|
|
||||||
- name: Check idempotency after upgrade
|
- name: "Upgrade {{ chart_test }} from {{ source }}"
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
chart_ref: "{{ chart_source_upgrade | default(chart_source) }}"
|
chart_ref: "{{ chart_source_upgrade | default(chart_source) }}"
|
||||||
chart_version: "{{ chart_source_version_upgrade | default(omit) }}"
|
chart_version: "{{ chart_source_version_upgrade | default(omit) }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
register: install
|
register: install
|
||||||
|
|
||||||
- name: Assert idempotency after upgrade
|
- name: "Assert that {{ chart_test }} chart is upgraded with new version from {{ source }}"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is not changed
|
- install is changed
|
||||||
- install.status.status | lower == 'deployed'
|
- install.status.status | lower == 'deployed'
|
||||||
- install.status.chart == "{{ chart_test }}-{{ chart_test_version_upgrade }}"
|
- install.status.chart == "{{ chart_test }}-{{ chart_test_version_upgrade }}"
|
||||||
|
|
||||||
- name: "Remove {{ chart_test }} from {{ source }}"
|
- name: Check idempotency after upgrade
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
state: absent
|
name: test
|
||||||
name: test
|
chart_ref: "{{ chart_source_upgrade | default(chart_source) }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
chart_version: "{{ chart_source_version_upgrade | default(omit) }}"
|
||||||
register: install
|
namespace: "{{ helm_namespace }}"
|
||||||
|
register: install
|
||||||
|
|
||||||
- name: "Assert that {{ chart_test }} chart is removed from {{ source }}"
|
- name: Assert idempotency after upgrade
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is changed
|
- install is not changed
|
||||||
|
- install.status.status | lower == 'deployed'
|
||||||
|
- install.status.chart == "{{ chart_test }}-{{ chart_test_version_upgrade }}"
|
||||||
|
|
||||||
- name: Check idempotency after remove
|
- name: "Remove {{ chart_test }} from {{ source }}"
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
state: absent
|
state: absent
|
||||||
name: test
|
name: test
|
||||||
namespace: "{{ helm_namespace }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
register: install
|
register: install
|
||||||
|
|
||||||
- name: Assert idempotency
|
- name: "Assert that {{ chart_test }} chart is removed from {{ source }}"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is not changed
|
- install is changed
|
||||||
|
|
||||||
# Test --replace
|
- name: Check idempotency after remove
|
||||||
- name: Install chart for replace option
|
helm:
|
||||||
helm:
|
binary_path: "{{ helm_binary }}"
|
||||||
binary_path: "{{ helm_binary }}"
|
state: absent
|
||||||
name: test-0001
|
name: test
|
||||||
chart_ref: "{{ chart_source }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
register: install
|
||||||
namespace: "{{ helm_namespace }}"
|
|
||||||
register: install
|
|
||||||
|
|
||||||
- name: "Assert that {{ chart_test }} chart is installed from {{ source }}"
|
- name: Assert idempotency
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is changed
|
- install is not changed
|
||||||
|
|
||||||
- name: Remove {{ chart_test }} with --purge
|
# Test --replace
|
||||||
helm:
|
- name: Install chart for replace option
|
||||||
binary_path: "{{ helm_binary }}"
|
helm:
|
||||||
state: absent
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test-0001
|
name: test-0001
|
||||||
purge: False
|
chart_ref: "{{ chart_source }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
register: install
|
namespace: "{{ helm_namespace }}"
|
||||||
|
register: install
|
||||||
|
|
||||||
- name: Check if chart is removed
|
- name: "Assert that {{ chart_test }} chart is installed from {{ source }}"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is changed
|
- install is changed
|
||||||
|
|
||||||
- name: Install chart again with same name test-0001
|
- name: Remove {{ chart_test }} with --purge
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test-0001
|
state: absent
|
||||||
chart_ref: "{{ chart_source }}"
|
name: test-0001
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
purge: False
|
||||||
namespace: "{{ helm_namespace }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
replace: True
|
register: install
|
||||||
register: install
|
|
||||||
|
|
||||||
- name: "Assert that {{ chart_test }} chart is installed from {{ source }}"
|
- name: Check if chart is removed
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is changed
|
- install is changed
|
||||||
|
|
||||||
- name: Remove {{ chart_test }} (cleanup)
|
- name: Install chart again with same name test-0001
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
state: absent
|
name: test-0001
|
||||||
name: test-0001
|
chart_ref: "{{ chart_source }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
register: install
|
namespace: "{{ helm_namespace }}"
|
||||||
|
replace: True
|
||||||
|
register: install
|
||||||
|
|
||||||
- name: Check if chart is removed
|
- name: "Assert that {{ chart_test }} chart is installed from {{ source }}"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is changed
|
- install is changed
|
||||||
|
|
||||||
- name: "Install {{ chart_test }} from {{ source }} with values_files"
|
- name: Remove {{ chart_test }} (cleanup)
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
state: absent
|
||||||
chart_ref: "{{ chart_source }}"
|
name: test-0001
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
register: install
|
||||||
values_files:
|
|
||||||
- "{{ role_path }}/files/values.yaml"
|
|
||||||
register: install
|
|
||||||
|
|
||||||
- name: "Assert that {{ chart_test }} chart has var from {{ source }}"
|
- name: Check if chart is removed
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- install is changed
|
- install is changed
|
||||||
- install.status.status | lower == 'deployed'
|
|
||||||
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
|
||||||
- "install.status['values'].revisionHistoryLimit == 0"
|
|
||||||
|
|
||||||
- name: "Install {{ chart_test }} from {{ source }} with values_files (again)"
|
- name: "Install {{ chart_test }} from {{ source }} with values_files"
|
||||||
helm:
|
helm:
|
||||||
binary_path: "{{ helm_binary }}"
|
binary_path: "{{ helm_binary }}"
|
||||||
name: test
|
name: test
|
||||||
chart_ref: "{{ chart_source }}"
|
chart_ref: "{{ chart_source }}"
|
||||||
chart_version: "{{ chart_source_version | default(omit) }}"
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
namespace: "{{ helm_namespace }}"
|
namespace: "{{ helm_namespace }}"
|
||||||
values_files:
|
values_files:
|
||||||
- "{{ role_path }}/files/values.yaml"
|
- "{{ role_path }}/files/values.yaml"
|
||||||
register: install
|
register: install
|
||||||
|
|
||||||
- name: "Assert the result is consistent"
|
- name: "Assert that {{ chart_test }} chart has var from {{ source }}"
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- not (install is changed)
|
- install is changed
|
||||||
|
- install.status.status | lower == 'deployed'
|
||||||
|
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
|
||||||
|
- "install.status['values'].revisionHistoryLimit == 0"
|
||||||
|
|
||||||
- name: Remove helm namespace
|
- name: "Install {{ chart_test }} from {{ source }} with values_files (again)"
|
||||||
k8s:
|
helm:
|
||||||
api_version: v1
|
binary_path: "{{ helm_binary }}"
|
||||||
kind: Namespace
|
name: test
|
||||||
name: "{{ helm_namespace }}"
|
chart_ref: "{{ chart_source }}"
|
||||||
state: absent
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
wait: true
|
namespace: "{{ helm_namespace }}"
|
||||||
wait_timeout: 180
|
values_files:
|
||||||
|
- "{{ role_path }}/files/values.yaml"
|
||||||
|
register: install
|
||||||
|
|
||||||
|
- name: "Assert the result is consistent"
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- not (install is changed)
|
||||||
|
|
||||||
|
- name: Render templates
|
||||||
|
helm_template:
|
||||||
|
binary_path: "{{ helm_binary }}"
|
||||||
|
chart_ref: "{{ chart_source }}"
|
||||||
|
chart_version: "{{ chart_source_version | default(omit) }}"
|
||||||
|
output_dir: "{{ temp_dir }}"
|
||||||
|
values_files:
|
||||||
|
- "{{ role_path }}/files/values.yaml"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- result is changed
|
||||||
|
- result is not failed
|
||||||
|
- result.rc == 0
|
||||||
|
- result.command is match("{{ helm_binary }} template {{ chart_source }}")
|
||||||
|
|
||||||
|
- name: Check templates created
|
||||||
|
stat:
|
||||||
|
path: "{{ temp_dir }}/{{ chart_test }}/templates"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
result.stat.exists
|
||||||
|
|
||||||
|
always:
|
||||||
|
- name: Clean up temp dir
|
||||||
|
file:
|
||||||
|
state: absent
|
||||||
|
path: "{{ temp_dir }}"
|
||||||
|
ignore_errors: true
|
||||||
|
|
||||||
|
- name: Remove helm namespace
|
||||||
|
k8s:
|
||||||
|
api_version: v1
|
||||||
|
kind: Namespace
|
||||||
|
name: "{{ helm_namespace }}"
|
||||||
|
state: absent
|
||||||
|
wait: true
|
||||||
|
wait_timeout: 180
|
||||||
|
|||||||
220
plugins/modules/helm_template.py
Normal file
220
plugins/modules/helm_template.py
Normal file
@@ -0,0 +1,220 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright: (c) 2021, Ansible Project
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function
|
||||||
|
|
||||||
|
__metaclass__ = type
|
||||||
|
|
||||||
|
|
||||||
|
DOCUMENTATION = r'''
|
||||||
|
|
||||||
|
module: helm_template
|
||||||
|
|
||||||
|
short_description: Render chart templates
|
||||||
|
|
||||||
|
author:
|
||||||
|
- Mike Graves (@gravesm)
|
||||||
|
|
||||||
|
description:
|
||||||
|
- Render chart templates to an output directory or as text of concatenated yaml documents.
|
||||||
|
|
||||||
|
options:
|
||||||
|
binary_path:
|
||||||
|
description:
|
||||||
|
- The path of a helm binary to use.
|
||||||
|
required: false
|
||||||
|
type: path
|
||||||
|
chart_ref:
|
||||||
|
description:
|
||||||
|
- Chart reference with repo prefix, for example, C(nginx-stable/nginx-ingress).
|
||||||
|
- Path to a packaged chart.
|
||||||
|
- Path to an unpacked chart directory.
|
||||||
|
- Absolute URL.
|
||||||
|
required: true
|
||||||
|
type: path
|
||||||
|
chart_repo_url:
|
||||||
|
description:
|
||||||
|
- Chart repository URL where the requested chart is located.
|
||||||
|
required: false
|
||||||
|
type: str
|
||||||
|
chart_version:
|
||||||
|
description:
|
||||||
|
- Chart version to use. If this is not specified, the latest version is installed.
|
||||||
|
required: false
|
||||||
|
type: str
|
||||||
|
include_crds:
|
||||||
|
description:
|
||||||
|
- Include custom resource descriptions in rendered templates.
|
||||||
|
required: false
|
||||||
|
type: bool
|
||||||
|
default: false
|
||||||
|
output_dir:
|
||||||
|
description:
|
||||||
|
- Output directory where templates will be written.
|
||||||
|
- If the directory already exists, it will be overwritten.
|
||||||
|
required: false
|
||||||
|
type: path
|
||||||
|
release_values:
|
||||||
|
description:
|
||||||
|
- Values to pass to chart.
|
||||||
|
required: false
|
||||||
|
default: {}
|
||||||
|
aliases: [ values ]
|
||||||
|
type: dict
|
||||||
|
values_files:
|
||||||
|
description:
|
||||||
|
- Value files to pass to chart.
|
||||||
|
- Paths will be read from the target host's filesystem, not the host running ansible.
|
||||||
|
- I(values_files) option is evaluated before I(values) option if both are used.
|
||||||
|
- Paths are evaluated in the order the paths are specified.
|
||||||
|
required: false
|
||||||
|
default: []
|
||||||
|
type: list
|
||||||
|
elements: str
|
||||||
|
update_repo_cache:
|
||||||
|
description:
|
||||||
|
- Run C(helm repo update) before the operation. Can be run as part of the template generation or as a separate step.
|
||||||
|
default: false
|
||||||
|
type: bool
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = r'''
|
||||||
|
- name: Render templates to specified directory
|
||||||
|
community.kubernetes.helm_template:
|
||||||
|
chart_ref: stable/prometheus
|
||||||
|
output_dir: mycharts
|
||||||
|
|
||||||
|
- name: Render templates
|
||||||
|
community.kubernetes.helm_template:
|
||||||
|
chart_ref: stable/prometheus
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- name: Write templates to file
|
||||||
|
copy:
|
||||||
|
dest: myfile.yaml
|
||||||
|
content: "{{ result.stdout }}"
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = r'''
|
||||||
|
stdout:
|
||||||
|
type: str
|
||||||
|
description: Full C(helm) command stdout. If no I(output_dir) has been provided this will contain the rendered templates as concatenated yaml documents.
|
||||||
|
returned: always
|
||||||
|
sample: ''
|
||||||
|
stderr:
|
||||||
|
type: str
|
||||||
|
description: Full C(helm) command stderr, in case you want to display it or examine the event log.
|
||||||
|
returned: always
|
||||||
|
sample: ''
|
||||||
|
command:
|
||||||
|
type: str
|
||||||
|
description: Full C(helm) command run by this module, in case you want to re-run the command outside the module or debug a problem.
|
||||||
|
returned: always
|
||||||
|
sample: helm template --output-dir mychart nginx-stable/nginx-ingress
|
||||||
|
'''
|
||||||
|
|
||||||
|
import tempfile
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
try:
|
||||||
|
import yaml
|
||||||
|
IMP_YAML = True
|
||||||
|
except ImportError:
|
||||||
|
IMP_YAML_ERR = traceback.format_exc()
|
||||||
|
IMP_YAML = False
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||||
|
from ansible_collections.community.kubernetes.plugins.module_utils.helm import run_helm
|
||||||
|
|
||||||
|
|
||||||
|
def template(cmd, chart_ref, chart_repo_url=None, chart_version=None, output_dir=None,
|
||||||
|
release_values=None, values_files=None, include_crds=False):
|
||||||
|
cmd += " template " + chart_ref
|
||||||
|
|
||||||
|
if chart_repo_url:
|
||||||
|
cmd += " --repo=" + chart_repo_url
|
||||||
|
|
||||||
|
if chart_version:
|
||||||
|
cmd += " --version=" + chart_version
|
||||||
|
|
||||||
|
if output_dir:
|
||||||
|
cmd += " --output-dir=" + output_dir
|
||||||
|
|
||||||
|
if release_values:
|
||||||
|
fd, path = tempfile.mkstemp(suffix='.yml')
|
||||||
|
with open(path, 'w') as yaml_file:
|
||||||
|
yaml.dump(release_values, yaml_file, default_flow_style=False)
|
||||||
|
cmd += " -f=" + path
|
||||||
|
|
||||||
|
if values_files:
|
||||||
|
for values_file in values_files:
|
||||||
|
cmd += " -f=" + values_file
|
||||||
|
|
||||||
|
if include_crds:
|
||||||
|
cmd += " --include-crds"
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
module = AnsibleModule(
|
||||||
|
argument_spec=dict(
|
||||||
|
binary_path=dict(type='path'),
|
||||||
|
chart_ref=dict(type='path', required=True),
|
||||||
|
chart_repo_url=dict(type='str'),
|
||||||
|
chart_version=dict(type='str'),
|
||||||
|
include_crds=dict(type='bool', default=False),
|
||||||
|
output_dir=dict(type='path'),
|
||||||
|
release_values=dict(type='dict', default={}, aliases=['values']),
|
||||||
|
values_files=dict(type='list', default=[], elements='str'),
|
||||||
|
update_repo_cache=dict(type='bool', default=False)
|
||||||
|
),
|
||||||
|
supports_check_mode=True
|
||||||
|
)
|
||||||
|
|
||||||
|
check_mode = module.check_mode
|
||||||
|
bin_path = module.params.get('binary_path')
|
||||||
|
chart_ref = module.params.get('chart_ref')
|
||||||
|
chart_repo_url = module.params.get('chart_repo_url')
|
||||||
|
chart_version = module.params.get('chart_version')
|
||||||
|
include_crds = module.params.get('include_crds')
|
||||||
|
output_dir = module.params.get('output_dir')
|
||||||
|
release_values = module.params.get('release_values')
|
||||||
|
values_files = module.params.get('values_files')
|
||||||
|
update_repo_cache = module.params.get('update_repo_cache')
|
||||||
|
|
||||||
|
if not IMP_YAML:
|
||||||
|
module.fail_json(msg=missing_required_lib("yaml"), exception=IMP_YAML_ERR)
|
||||||
|
|
||||||
|
helm_cmd = bin_path or module.get_bin_path('helm', required=True)
|
||||||
|
|
||||||
|
if update_repo_cache:
|
||||||
|
update_cmd = helm_cmd + " repo update"
|
||||||
|
run_helm(module, update_cmd)
|
||||||
|
|
||||||
|
tmpl_cmd = template(helm_cmd, chart_ref, chart_repo_url=chart_repo_url,
|
||||||
|
chart_version=chart_version, output_dir=output_dir,
|
||||||
|
release_values=release_values, values_files=values_files,
|
||||||
|
include_crds=include_crds)
|
||||||
|
|
||||||
|
if not check_mode:
|
||||||
|
rc, out, err = run_helm(module, tmpl_cmd)
|
||||||
|
else:
|
||||||
|
out = err = ""
|
||||||
|
rc = 0
|
||||||
|
|
||||||
|
module.exit_json(
|
||||||
|
failed=False,
|
||||||
|
changed=True,
|
||||||
|
command=tmpl_cmd,
|
||||||
|
stdout=out,
|
||||||
|
stderr=err,
|
||||||
|
rc=rc
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user