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 will remove variables if a value is not specified #40450

Closed
unacceptable opened this issue May 20, 2018 · 4 comments · Fixed by #40468
Closed

win_environment will remove variables if a value is not specified #40450

unacceptable opened this issue May 20, 2018 · 4 comments · Fixed by #40468
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:community This issue/PR relates to code supported by the Ansible community. windows Windows community

Comments

@unacceptable
Copy link

ISSUE TYPE
  • Bug Report
COMPONENT NAME

win_environment

ANSIBLE VERSION
# ansible --version
ansible 2.5.3
  config file = /root/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
CONFIGURATION
# ansible-config dump --only-changed
DEFAULT_FORKS(/root/ansible/ansible.cfg) = 60
DEFAULT_HOST_LIST(/root/ansible/ansible.cfg) = [u'/root/ansible/inventory']
DEFAULT_VAULT_PASSWORD_FILE(/root/ansible/ansible.cfg) = /root/ansible/secret
RETRY_FILES_ENABLED(/root/ansible/ansible.cfg) = False
OS / ENVIRONMENT
# cat /etc/*release
CentOS Linux release 7.4.1708 (Core) 
...
SUMMARY
  • Firstly I wasn't sure if I should open a feature request or a bug. However, after much thought about this, a bug seems appropriate since data is being unintentionally removed from a system.
  • win_environment will remove variables if a value is not specified.
STEPS TO REPRODUCE

Set an environment variable ServerRole in a separate playbook or role. Then run the following code:

- name: Get ServerRole Variable
  win_environment:
    name: ServerRole
    level: machine
  register: server_role
  when: ansible_os_family == 'Windows'
EXPECTED RESULTS

I would expect no change as I did not specify a value and did not specify a state of absent.

ACTUAL RESULTS

This will remove the variable.

changed: [W039.local] => {
    "before_value": "Web", 
    "changed": true, 
    "value": null
}

I now that I can pull this from the win_shell module with something like this:

- name: Get ServerRole Variable
  win_shell: |
    $env:ServerRole
  changed_when: false
  register: server_role

Then to use it this would look like:

{{ server_role.stdout_lines[-1] }

But as demonstrated this is not very elegant.

@ansibot
Copy link
Contributor

ansibot commented May 20, 2018

Files identified in the description:

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot
Copy link
Contributor

ansibot commented May 20, 2018

@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. support:community This issue/PR relates to code supported by the Ansible community. windows Windows community labels May 20, 2018
@jborean93
Copy link
Contributor

jborean93 commented May 20, 2018

I wouldn't call this a bug because it doesn't return the value without changing but I would say it is a bug because it removes the environment variable altogether. This is because the default state option is present and in this case it is removing the env var instead of just setting the value to an empty string.

We wouldn't want to change that behaviour to only display the value is value is not set as that is not what state=present implies.

For your use case, you have 2 ways of doing what you want, the first is to use a win_shell task like you mentioned while the other is to use the setup module to gather/refresh facts about the machine like so

# this will work but it gathers all the host facts and can take a longer time than the task below
- name: update all the host facts
  setup:

# this will be quicker if you only want an updated set of env variables
- name: update only the environment variable host facts
  setup:
    gather_subset: env

- name: display the value for the ServerRole env variable
  debug:
    msg: ServerRole={{ansible_env.ServerRole}}

@ansibot ansibot removed the needs_triage Needs a first human triage before being processed. label May 20, 2018
@jborean93
Copy link
Contributor

@ScriptMyJob turns out Windows doesn't support null or empty strings as an environment value. I've raised a PR to update the docs and throw an actual error message if someone tries to do this with state: present here #40468. My comments around retrieving variables still stand though, use either the builtin facts or a win_shell task to query them.

@ansible ansible locked and limited conversation to collaborators May 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:community This issue/PR relates to code supported by the Ansible community. windows Windows community
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants