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

win_environment: Added explicit check for null, empty string with state=present #40468

Merged
merged 2 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- win_environment - Fix for issue where the environment value was deleted when a null value or empty string was set - https://github.com/ansible/ansible/issues/40450
2 changes: 2 additions & 0 deletions lib/ansible/modules/windows/win_environment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ $result = @{
if ($state -eq "absent" -and $value) {
Add-Warning -obj $result -message "When removing environment variable '$name' it should not have a value '$value' set"
$value = $null
} elseif ($state -eq "present" -and (-not $value)) {
Fail-Json -obj $result -message "When state=present, value must be defined and not an empty string, if you wish to remove the envvar, set state=absent"
}

if ($state -eq "present" -and $before_value -ne $value) {
Expand Down
1 change: 1 addition & 0 deletions lib/ansible/modules/windows/win_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
value:
description:
- The value to store in the environment variable.
- Must be set when C(state=present) and cannot be an empty string.
- Can be omitted for C(state=absent).
level:
description:
Expand Down
17 changes: 17 additions & 0 deletions test/integration/targets/win_environment/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
- process
- user

- name: fail to create environment value with null value
win_environment:
name: "{{test_environment_name}}"
state: present
level: machine
register: create_fail_null
failed_when: create_fail_null.msg != "When state=present, value must be defined and not an empty string, if you wish to remove the envvar, set state=absent"

- name: fail to create environment value with empty value
win_environment:
name: "{{test_environment_name}}"
value: ''
state: present
level: machine
register: create_fail_empty_string
failed_when: create_fail_null.msg != "When state=present, value must be defined and not an empty string, if you wish to remove the envvar, set state=absent"

- name: create test environment value for machine check
win_environment:
name: "{{test_environment_name}}"
Expand Down