Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json-ify environment values #61295

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/environment-dict.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- Setting an environment value on a task that is a dict or list will be converted to JSON - https://github.com/ansible/ansible/issues/60956
8 changes: 8 additions & 0 deletions lib/ansible/plugins/action/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,17 @@ def _compute_environment_string(self, raw_environment_out=None):
for environment in environments:
if environment is None or len(environment) == 0:
continue

temp_environment = self._templar.template(environment)
if not isinstance(temp_environment, dict):
raise AnsibleError("environment must be a dictionary, received %s (%s)" % (temp_environment, type(temp_environment)))

# Ensure any env values that are dicts are converted to JSON
# https://github.com/ansible/ansible/issues/60956
for env_key, env_val in temp_environment.items():
if isinstance(env_val, dict) or isinstance(env_val, list):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Mapping and Sequence classes instead as you might get an AnsibleSequence or similar object that does not inherit directly from 'list`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the is_sequence function would be preferred, to make sure it isn't picking up strings too.

temp_environment[env_key] = json.dumps(env_val)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while this might make sense for dictionaries, since shell doesn't really support them, for lists it might not as they are supported by shells


# very deliberately using update here instead of combine_vars, as
# these environment settings should not need to merge sub-dicts
final_environment.update(temp_environment)
Expand Down
24 changes: 24 additions & 0 deletions test/integration/targets/command_shell/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,27 @@
file:
path: "{{ output_dir_test }}/afile.txt"
state: absent

# https://github.com/ansible/ansible/issues/60956
- set_fact:
some_var: some_value

- set_fact:
bla:
foo: bar
aaa: '{{ some_var }}'

- name: test out dict/list as json var
shell: env | grep testvar
register: shell_dict_env
environment:
testvar: '{{ item }}'
loop:
- '{{ bla }}'
- [ '{{ bla }}' ]

- name: assert test var was passed through correctly
assert:
that:
- 'shell_dict_env.results[0].stdout_lines == [''testvar={"foo": "bar", "aaa": "some_value"}'']'
- 'shell_dict_env.results[1].stdout_lines == [''testvar=[{"foo": "bar", "aaa": "some_value"}]'']'
24 changes: 24 additions & 0 deletions test/integration/targets/win_shell/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,27 @@
win_file:
path: C:\ansible test link
state: absent

# https://github.com/ansible/ansible/issues/60956
- set_fact:
some_var: some_value

- set_fact:
bla:
foo: bar
aaa: '{{ some_var }}'

- name: test out dict/list as json var
win_shell: $env:testvar
register: shell_dict_env
environment:
testvar: '{{ item }}'
loop:
- '{{ bla }}'
- [ '{{ bla }}' ]

- name: assert test var was passed through correctly
assert:
that:
- 'shell_dict_env.results[0].stdout_lines == [''{"foo": "bar", "aaa": "some_value"}'']'
- 'shell_dict_env.results[1].stdout_lines == [''[{"foo": "bar", "aaa": "some_value"}]'']'