# Mind-boggling variable expansion in problems in 'lookup'

**URL:** <https://forum.ansible.com/t/mind-boggling-variable-expansion-in-problems-in-lookup/14123>\
**Category:** Ansible Project\
**Created:** [December 11, 2013, 6:19am UTC](https://forum.ansible.com/t/mind-boggling-variable-expansion-in-problems-in-lookup/14123 "2013-12-11T06:19:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman\_Revyakin](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@Roman\_Revyakin](https://forum.ansible.com/u/Roman_Revyakin)\
**Post date:** [December 11, 2013, 6:19am UTC](https://forum.ansible.com/t/mind-boggling-variable-expansion-in-problems-in-lookup/14123/1 "2013-12-11T06:19:45Z")

</div>

Hi guys,

Can anyone tell me why variables in this simple playbook do not get expanded?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 11, 2013, 1:20pm UTC](https://forum.ansible.com/t/mind-boggling-variable-expansion-in-problems-in-lookup/14123/2 "2013-12-11T13:20:40Z")

</div>

Hi Roman,

Happy to help.

Small request – can you please send just text based mail in the future? It would make this a lot easier to read. Thanks!

Quick public service announcement, assuming you are using a recent Ansible, you should simplify your clauses:

when: none\_name not in node\_query.json.computer | map(attribute=‘displayName’) | list

The need to do all the {{ dereference }} is not needed in recent Ansible and can cause problems.

Also, your lookup is should not nest templates, that won’t work.

It should look like:

lookup(‘file’, ‘/tmp/’ + node\_name + ‘\_newslave.json’)

I’m pretty sure you can’t urlencode right there inside the lookup statement either, so I’d recommend this:

set\_fact: my\_path={{ ‘/tmp’ + node\_name + ‘\_newslave.json’ | urlencode }}

Save that variable and then it looks much much cleaner!

{{ lookup(‘file’, my\_path) }}

I would also define a variable for the URL so your playbooks can be cleaner.

Also, in the future please report what Ansible version you are using when you have questions, as that can sometimes be relevant.

Thanks and hope this helps, let us know if you have more questions!

–Michael

---

<div class="post-metadata">

**Author:** ![Roman\_Revyakin](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@Roman\_Revyakin](https://forum.ansible.com/u/Roman_Revyakin)\
**Post date:** [December 11, 2013, 9:47pm UTC](https://forum.ansible.com/t/mind-boggling-variable-expansion-in-problems-in-lookup/14123/3 "2013-12-11T21:47:56Z")

</div>

Hi Michael,

Thanks for your reply - and my apologies for pasting with all the syntax colours and backgrounds - I would have removed those if I was be able to preview how ugly my post would look like 🙂

15 minutes past my submit I was able to figure out myself that my claim that this piece of code works was wrong:

local\_action: uri user={{ jenkins.admin }} password={{ jenkins.password }} method=POST url=“http://{{ jenkins.master\_host }}:{{ jenkins.master\_port }}/computer/doCreateItem?name={{ node\_name }}&type=hudson.slaves.DumbSlave&json={{ lookup(‘file’, ‘/tmp/{{ node\_name }}\_newslave.json’ )|urlencode }}” force\_basic\_auth=yes return\_content=yes status\_code=302

so I had to rewrite it as

local\_action: uri user=‘{{ jenkins.admin }}’ password=‘{{ jenkins.password }}’ method=POST url=“http://{{ jenkins.master\_host }}:{{ jenkins.master\_port }}/computer/doCreateItem?name={{ node\_name }}&type=hudson.slaves.DumbSlave&json={{ lookup(‘file’, ‘/tmp/’ ~ node\_name ~ ‘\_newslave.json’ )|urlencode }}” force\_basic\_auth=yes return\_content=yes status\_code=302

along the lines of what you proposed. Unfortunately I was not able to edit or reply to my post, as it was not approved as yet.  
And yes, urlencode works there inside {{}} after the lookup. But thanks for the hint about the `set_fact:`, it is handy indeed - I was in fact looking for something like that and somehow missed to spot it on the [Ansible modules page](http://www.ansibleworks.com/docs/modules.html#set-fact). May be it’s worthwhile to mention it on a [Best Practices page](http://www.ansibleworks.com/docs/playbooks_best_practices.html)?

And yes, I am using the ‘develop’ branch of Ansible - and will be stating the version in my next posts should they occur.

Thanks again for your time and hints,

Best regards,  
Roman
