Fix env lookup plugin error on utf8 values (#65541)

This commit fixes issue #65297.

The env lookup plugin used to fail when environment variable value
contained any UTF-8 characters (e.g., δ, ζ).
pull/60679/head
Abhay Kadam 5 years ago committed by Abhijeet Kasurde
parent 05e2e18061
commit 2fa8f9cfd8

@ -0,0 +1,2 @@
bugfixes:
- env lookup plugin - Fix handling of environment variables values containing utf-8 characters. (https://github.com/ansible/ansible/issues/65298)

@ -27,9 +27,8 @@ RETURN = """
- values from the environment variables. - values from the environment variables.
type: list type: list
""" """
import os
from ansible.plugins.lookup import LookupBase from ansible.plugins.lookup import LookupBase
from ansible.utils import py3compat
class LookupModule(LookupBase): class LookupModule(LookupBase):
@ -39,6 +38,6 @@ class LookupModule(LookupBase):
ret = [] ret = []
for term in terms: for term in terms:
var = term.split()[0] var = term.split()[0]
ret.append(os.getenv(var, '')) ret.append(py3compat.environ.get(var, ''))
return ret return ret

@ -8,7 +8,8 @@ source virtualenv.sh
# because plugins and requirements are loaded before the task runs # because plugins and requirements are loaded before the task runs
pip install passlib pip install passlib
ANSIBLE_ROLES_PATH=../ ansible-playbook lookups.yml "$@" # UNICODE_VAR is used in testing the env lookup plugin unicode functionality
ANSIBLE_ROLES_PATH=../ UNICODE_VAR=café ansible-playbook lookups.yml "$@"
ansible-playbook template_lookup_vaulted.yml --vault-password-file test_vault_pass "$@" ansible-playbook template_lookup_vaulted.yml --vault-password-file test_vault_pass "$@"

@ -154,6 +154,25 @@
that: that:
- "test_val == home_var_value.stdout" - "test_val == home_var_value.stdout"
# UNICODE LOOKUP
# https://github.com/ansible/ansible/issues/65297
- name: get UNICODE_VAR environment var value
shell: "echo $UNICODE_VAR"
register: unicode_var_value
- name: use env lookup to get UNICODE_VAR value
set_fact:
test_unicode_val: "{{ lookup('env', 'UNICODE_VAR') }}"
- debug: var=unicode_var_value
- debug: var=test_unicode_val
- name: compare unicode values
assert:
that:
- "test_unicode_val == unicode_var_value.stdout"
# PIPE LOOKUP # PIPE LOOKUP

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Copyright: (c) 2019, Abhay Kadam <abhaykadam88@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
from ansible.plugins.loader import lookup_loader
@pytest.mark.parametrize('env_var,exp_value', [
('foo', 'bar'),
('equation', 'a=b*100')
])
def test_env_var_value(monkeypatch, env_var, exp_value):
monkeypatch.setattr('ansible.utils.py3compat.environ.get', lambda x, y: exp_value)
env_lookup = lookup_loader.get('env')
retval = env_lookup.run([env_var], None)
assert retval == [exp_value]
@pytest.mark.parametrize('env_var,exp_value', [
('simple_var', 'alpha-β-gamma'),
('the_var', 'ãnˈsiβle')
])
def test_utf8_env_var_value(monkeypatch, env_var, exp_value):
monkeypatch.setattr('ansible.utils.py3compat.environ.get', lambda x, y: exp_value)
env_lookup = lookup_loader.get('env')
retval = env_lookup.run([env_var], None)
assert retval == [exp_value]
Loading…
Cancel
Save