From 7e70deb7348678d3b2929aacd551feb6447e738b Mon Sep 17 00:00:00 2001 From: Andrew Gaffney Date: Thu, 23 Apr 2020 10:57:52 -0500 Subject: [PATCH] Allow unsetting env vars (#7) --- plugins/shell/csh.py | 8 +++++++- plugins/shell/fish.py | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/plugins/shell/csh.py b/plugins/shell/csh.py index 4836dce..18dee95 100644 --- a/plugins/shell/csh.py +++ b/plugins/shell/csh.py @@ -4,6 +4,8 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +from ansible.module_utils.six import text_type +from ansible.module_utils.six.moves import shlex_quote from ansible.plugins.shell import ShellBase DOCUMENTATION = ''' @@ -35,4 +37,8 @@ class ShellModule(ShellBase): _SHELL_GROUP_RIGHT = ')' def env_prefix(self, **kwargs): - return 'env %s' % super(ShellModule, self).env_prefix(**kwargs) + ret = [] + # All the -u options must be first, so we process them first + ret += ['-u %s' % k for k, v in kwargs.items() if v is None] + ret += ['%s=%s' % (k, shlex_quote(text_type(v))) for k, v in kwargs.items() if v is not None] + return 'env %s' % ' '.join(ret) diff --git a/plugins/shell/fish.py b/plugins/shell/fish.py index 8d0a4aa..ea13358 100644 --- a/plugins/shell/fish.py +++ b/plugins/shell/fish.py @@ -38,7 +38,13 @@ class ShellModule(ShModule): def env_prefix(self, **kwargs): env = self.env.copy() env.update(kwargs) - return ' '.join(['set -lx %s %s;' % (k, shlex_quote(text_type(v))) for k, v in env.items()]) + ret = [] + for k, v in kwargs.items(): + if v is None: + ret.append('set -e %s;' % k) + else: + ret.append('set -lx %s %s;' % (k, shlex_quote(text_type(v)))) + return ' '.join(ret) def build_module_command(self, env_string, shebang, cmd, arg_path=None): # don't quote the cmd if it's an empty string, because this will break pipelining mode