Skip to content

Commit

Permalink
Stringify all env vars, not just those from file (#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed May 3, 2022
1 parent 49237b8 commit c143c07
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion ansible_runner/config/_base.py
Expand Up @@ -234,7 +234,7 @@ def _prepare_env(self, runner_mode='pexpect'):
try:
envvars = self.loader.load_file('env/envvars', Mapping)
if envvars:
self.env.update({str(k): str(v) for k, v in envvars.items()})
self.env.update(envvars)
except ConfigurationError:
debug("Not loading environment vars")
# Still need to pass default environment to pexpect
Expand Down Expand Up @@ -293,6 +293,9 @@ def _prepare_env(self, runner_mode='pexpect'):
if not self.containerized:
self.env['ANSIBLE_CACHE_PLUGIN_CONNECTION'] = self.fact_cache

# Pexpect will error with non-string envvars types, so we ensure string types
self.env = {str(k): str(v) for k, v in self.env.items()}

debug('env:')
for k, v in sorted(self.env.items()):
debug(f' {k}: {v}')
Expand Down
16 changes: 15 additions & 1 deletion test/unit/config/test__base.py
Expand Up @@ -73,7 +73,7 @@ def test_base_config_project_dir(tmp_path):
assert rc.project_dir == tmp_path.joinpath('project').as_posix()


def test_prepare_environment_vars_only_strings(mocker):
def test_prepare_environment_vars_only_strings_from_file(mocker):
rc = BaseConfig(envvars=dict(D='D'))

value = dict(A=1, B=True, C="foo")
Expand All @@ -92,6 +92,20 @@ def test_prepare_environment_vars_only_strings(mocker):
assert rc.env['D'] == 'D'


def test_prepare_environment_vars_only_strings_from_interface():
rc = BaseConfig(envvars=dict(D='D', A=1, B=True, C="foo"))
rc._prepare_env()

assert 'A' in rc.env
assert isinstance(rc.env['A'], six.string_types)
assert 'B' in rc.env
assert isinstance(rc.env['B'], six.string_types)
assert 'C' in rc.env
assert isinstance(rc.env['C'], six.string_types)
assert 'D' in rc.env
assert rc.env['D'] == 'D'


def test_prepare_environment_pexpect_defaults():
rc = BaseConfig()
rc._prepare_env()
Expand Down

0 comments on commit c143c07

Please sign in to comment.