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

[1.6.1] Value getting lost when a variable name is HOME #7396

Closed
jayyvis opened this issue May 14, 2014 · 8 comments · Fixed by #7481
Closed

[1.6.1] Value getting lost when a variable name is HOME #7396

jayyvis opened this issue May 14, 2014 · 8 comments · Fixed by #7481
Labels
bug This issue/PR relates to a bug. P2 Priority 2 - Issue Blocks Release

Comments

@jayyvis
Copy link

jayyvis commented May 14, 2014

Issue Type:

“Bug Report”

Ansible Version:

ansible 1.6.1

Environment:

Debian / local connection

Summary:

I am creating a variable named HOME and setting it to user's home directory by looking up environment. In ansible 1.6.1, the value of this variable is becoming empty string during the playbook execution.

Steps To Reproduce:

Run the following playbook and checkout the result of second debug task.

ansible-playbook homedir.yml


---
- hosts: all
  gather_facts: False
  vars:
    HOME: "{{lookup('env','HOME')}}"
  tasks:
    - debug: var=HOME
    - debug: var=HOME
Expected Results:
PLAY [all] ******************************************************************** 

TASK: [debug var=HOME] ******************************************************** 
ok: [localhost] => {
    "HOME": "/home/jay"
}

TASK: [debug var=HOME] ******************************************************** 
ok: [localhost] => {
    "HOME": "/home/jay"
}

PLAY RECAP ******************************************************************** 
localhost                  : ok=2    changed=0    unreachable=0    failed=0   
Actual Results:
PLAY [all] ******************************************************************** 

TASK: [debug var=HOME] ******************************************************** 
ok: [localhost] => {
    "HOME": "/home/jay"
}

TASK: [debug var=HOME] ******************************************************** 
ok: [localhost] => {
    "HOME": ""
}

PLAY RECAP ******************************************************************** 
localhost                  : ok=2    changed=0    unreachable=0    failed=0   
@mpdehaan mpdehaan added P3 and removed P3 labels May 14, 2014
@jimi-c
Copy link
Member

jimi-c commented May 14, 2014

I can't reproduce this. On my system, I get an empty string for both cases, however if I use the lookup to get SHELL instead it works fine and I see the same value for both debugs. Can you provide any other information regarding your setup that might help in debugging this?

@jayyvis
Copy link
Author

jayyvis commented May 15, 2014

Provisioned a new ubuntu 13.10 64-bit server and installed ansible 1.6.1. And I am able to reproduce the issue for SHELL lookup as well.

It happens when ansible variable name is same as the environment variable name.

---
- hosts: all
  gather_facts: False
  vars:
    SHELL: "{{lookup('env','SHELL')}}"
  tasks:
    - debug: var=SHELL
    - debug: var=SHELL
PLAY [all] ******************************************************************** 

TASK: [debug var=SHELL] ******************************************************* 
ok: [localhost] => {
    "SHELL": "/bin/bash"
}

TASK: [debug var=SHELL] ******************************************************* 
ok: [localhost] => {
    "SHELL": ""
}

PLAY RECAP ******************************************************************** 
localhost                  : ok=2    changed=0    unreachable=0    failed=0   

@jimi-c
Copy link
Member

jimi-c commented May 16, 2014

This is turning out to be an odd bug, however I see why it is happening. Each time the variable is accessed, the lookup plugin is run. During each run, the return value is passed through the template engine, in case the lookup was specified with variables (ie. lookup('env', "{{ FOO }}")). Because of this, on subsequent passes the variable is overwritten by a blank value, which is the part I'm still debugging.

For now, the workaround for this is to avoid using a variable name which exactly matches the value of the lookup you're doing (ie. use ENV_HOME or something similar). I will continue debugging this to see if we can short-circuit the templating of the value.

@jayyvis
Copy link
Author

jayyvis commented May 17, 2014

Thank you. I'm already doing workaround by using different variable name.

BTW it used to work even in the development branch of 1.6. I suspect this
is a regression from recent commits.
On May 17, 2014 1:58 AM, "James Cammarata" notifications@github.com wrote:

This is turning out to be an odd bug, however I see why it is happening.
Each time the variable is accessed, the lookup plugin is run. During each
run, the return value is passed through the template engine, in case the
lookup was specified with variables (ie. lookup('env', "{{ FOO }}")).
Because of this, on subsequent passes the variable is overwritten by a
blank value, which is the part I'm still debugging.

For now, the workaround for this is to avoid using a variable name which
exactly matches the value of the lookup you're doing (ie. use ENV_HOME or
something similar). I will continue debugging this to see if we can
short-circuit the templating of the value.


Reply to this email directly or view it on GitHubhttps://github.com//issues/7396#issuecomment-43375766
.

@jimi-c
Copy link
Member

jimi-c commented May 19, 2014

Thanks, I'll do a bisect and see if I can see where it started acting this way.

@jimi-c
Copy link
Member

jimi-c commented May 20, 2014

I have a preliminary patch for this, though it may still need some additional testing:

diff --git a/lib/ansible/runner/lookup_plugins/env.py b/lib/ansible/runner/lookup_plugins/env.py
index 6acc5f5..d4f8535 100644
--- a/lib/ansible/runner/lookup_plugins/env.py
+++ b/lib/ansible/runner/lookup_plugins/env.py
@@ -16,6 +16,7 @@
 # along with Ansible.  If not, see .
 
 from ansible import utils, errors
+from ansible.utils import template
 import os
 
 class LookupModule(object):
@@ -25,7 +26,10 @@ class LookupModule(object):
 
     def run(self, terms, inject=None, **kwargs):
 
-        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) 
+        try:
+            terms = template.template(self.basedir, terms, inject)
+        except Exception, e:
+            pass
 
         if isinstance(terms, basestring):
             terms = [ terms ]

jimi-c added a commit to jimi-c/ansible that referenced this issue May 20, 2014
…bles

This was causing a bug in the env module, due to the fact that we now
pass variables for the module through the templating engine combined
with the fact that we split-up the hostvars and setup variables. As a
result, if a variable in the env lookup had the same name as the variable
in Ansible, it would try and template itself over and over again until
the recursion limit would be hit, at which time an empty string was
returned.

Fixes ansible#7396
@jimi-c jimi-c removed the needs_info label May 20, 2014
@jimi-c
Copy link
Member

jimi-c commented May 21, 2014

I have merged the above fix in, which should resolve this. Please let us know if you continue to see any problems related to this issue, or if you have any further questions. Thanks!

@jayyvis
Copy link
Author

jayyvis commented May 22, 2014

Tested and confirmed at my side. This issue is resolved now. Thanks a lot.

jimi-c added a commit that referenced this issue Jun 9, 2014
…bles

This was causing a bug in the env module, due to the fact that we now
pass variables for the module through the templating engine combined
with the fact that we split-up the hostvars and setup variables. As a
result, if a variable in the env lookup had the same name as the variable
in Ansible, it would try and template itself over and over again until
the recursion limit would be hit, at which time an empty string was
returned.

Fixes #7396
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 6, 2018
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug. P2 Priority 2 - Issue Blocks Release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants