diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index e484e36d..3ac986cc 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -118,6 +118,11 @@ description: - Timeout when wait option is enabled (helm2 is a number of seconds, helm3 is a duration). type: str + atomic: + description: + - If set, the installation process deletes the installation on failure. + type: bool + default: False ''' EXAMPLES = r''' @@ -230,6 +235,7 @@ sample: helm upgrade ... """ +import tempfile import traceback try: @@ -320,44 +326,37 @@ def fetch_chart_info(command, chart_ref): return yaml.safe_load(out) -def deploy(command, release_name, release_values, chart_name, wait, wait_timeout, disable_hook, force): +def deploy(**kwargs): """ Install/upgrade/rollback release chart """ - deploy_command = command + " upgrade -i" # install/upgrade + + deploy_command = kwargs['command'] + " upgrade -i" # install/upgrade # Always reset values to keep release_values equal to values released deploy_command += " --reset-values" - if wait: + if kwargs['wait']: deploy_command += " --wait" - if wait_timeout is not None: - deploy_command += " --timeout " + wait_timeout + if kwargs['wait_timeout'] is not None: + deploy_command += " --timeout " + kwargs['wait_timeout'] - if force: + if kwargs['force']: deploy_command += " --force" - if disable_hook: + if kwargs['disable_hook']: deploy_command += " --no-hooks" - if release_values != {}: - try: - import tempfile - except ImportError: - module.fail_json( - msg=missing_required_lib("tempfile"), - exception=traceback.format_exc(), - stdout='', - stderr='', - command='', - ) + if kwargs['atomic']: + deploy_command += " --atomic" + if kwargs['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) + yaml.dump(kwargs['release_values'], yaml_file, default_flow_style=False) deploy_command += " -f=" + path - deploy_command += " " + release_name + " " + chart_name + deploy_command += " " + kwargs['release_name'] + " " + kwargs['chart_name'] return deploy_command @@ -402,6 +401,7 @@ def main(): purge=dict(type='bool', default=True), wait=dict(type='bool', default=False), wait_timeout=dict(type='str'), + atomic=dict(type='bool', default=False), ), required_if=[ ('release_state', 'present', ['release_name', 'chart_ref']), @@ -469,15 +469,26 @@ def main(): # Fetch chart info to have real version and real name for chart_ref from archive, folder or url chart_info = fetch_chart_info(helm_cmd, chart_ref) + options = { + 'command': helm_cmd, + 'release_name': release_name, + 'release_values': release_values, + 'chart_name': chart_ref, + 'wait': wait, + 'wait_timeout': wait_timeout, + 'disable_hook': disable_hook, + 'force': False, + 'atomic': module.params.get('atomic'), + } + if release_status is None: # Not installed - helm_cmd = deploy(helm_cmd, release_name, release_values, chart_ref, wait, wait_timeout, - disable_hook, False) + helm_cmd = deploy(**options) changed = True elif force or release_values != release_status['values'] \ or (chart_info['name'] + '-' + chart_info['version']) != release_status["chart"]: - helm_cmd = deploy(helm_cmd, release_name, release_values, chart_ref, wait, wait_timeout, - disable_hook, force) + options['force'] = force + helm_cmd = deploy(**options) changed = True if module.check_mode: