# Variable file path in lookup

**URL:** <https://forum.ansible.com/t/variable-file-path-in-lookup/13688>\
**Category:** Ansible Project\
**Created:** [October 15, 2013, 9:45pm UTC](https://forum.ansible.com/t/variable-file-path-in-lookup/13688 "2013-10-15T21:45:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Carter](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Chris\_Carter](https://forum.ansible.com/u/Chris_Carter)\
**Post date:** [October 15, 2013, 9:45pm UTC](https://forum.ansible.com/t/variable-file-path-in-lookup/13688/1 "2013-10-15T21:45:39Z")

</div>

Hi there,

I’m attempting to configure a variable lookup to a public key file, and am running into some trouble. What confuses me is that the variable substitution works fine in a debug statement, but then blows up in the lookup statement for assigning authorized key values. Here’s the general idea;

## provisioning/git.yml (playbook):

> * * *
> 
> - hosts: git
> 
> sudo: true
> 
> roles:
> 
> - { role: git, git\_users: [‘me’] }

## provisioning/roles/git/tasks/main.yml:

> * * *
> 
> - user: name=git state=present home=/opt/git
> 
> - debug: msg={{ ‘../../users/files/’ + item + ‘.pub’ }}
> 
> with\_items: git\_users
> 
> - authorized\_key: user=git key={{ lookup(‘file’, ‘../../users/files/’ + item + ‘.pub’) }}
> 
> with\_items: git\_users

When I run ansible against the git.yml playbook, I end up with the following:

> TASK: [debug msg=../../users/files/.pub] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> ok: [[dev-build-c.oedev.us](http://dev-build-c.oedev.us)] =\> (item=me) =\> {“item”: “me”, “msg”: “../../users/files/me.pub”}
> 
> ERROR: blah/blah/blah/provisioning/roles/users/files/.pub does not exist

I’m really struggling with understanding what I am missing in terms of looping and the lookup functionality. We’re using lookups elsewhere with dynamically constructed paths for passwords, and it seems to work like I’d expect. But when I introduce a loop, it falls apart.

Thanks for any pointers to relieve me of my ignorance!

Chris

---

<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:** [October 16, 2013, 7:55pm UTC](https://forum.ansible.com/t/variable-file-path-in-lookup/13688/2 "2013-10-16T19:55:34Z")

</div>

I don’t see this as blowing up, I see this as reporting an error that it couldn’t find the file.

Help me understand why the file is not where you think it is?

---

<div class="post-metadata">

**Author:** ![Chris\_Carter](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Chris\_Carter](https://forum.ansible.com/u/Chris_Carter)\
**Post date:** [October 16, 2013, 8:21pm UTC](https://forum.ansible.com/t/variable-file-path-in-lookup/13688/3 "2013-10-16T20:21:30Z")

</div>

In the debug and error lines:

> ok: [[dev-build-c.oedev.us](http://dev-build-c.oedev.us/)] =\> (item=me) =\> {“item”: “me”, “msg”: “…/…/users/files/me.pub”}
> 
> ERROR: blah/blah/blah/provisioning/roles/users/files/.pub does not exist

The item it’s supposed to be supplying is “me”, so I would expect the path it derives for the lookup to be “…/…/users/files/me.pub”, but instead it’s hitting the lookup as “…/…/users/files/.pub”, and the portion where the replacement would occur is blank (hence it’s “.pub” and not “me.pub”. Does that make sense?

---

<div class="post-metadata">

**Author:** ![Chris\_Carter](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Chris\_Carter](https://forum.ansible.com/u/Chris_Carter)\
**Post date:** [October 16, 2013, 8:35pm UTC](https://forum.ansible.com/t/variable-file-path-in-lookup/13688/4 "2013-10-16T20:35:04Z")

</div>

I’ve managed to create a test case for this (these files fall under an ansible-test directory):

> **## looptest.yml**
> 
> * * *
> 
> - hosts: 127.0.0.1
> 
> connection: local
> 
> vars:
> 
> mylist:
> 
> - “item1”
> 
> roles:
> 
> - looptest

> **## roles/looptest/tasks/main.yml**
> 
> * * *
> 
> - debug: msg=“{{ item }}”
> 
> with\_items: mylist
> 
> - debug: msg=“{{ lookup(‘file’, ‘…/…/filerole/files/item1.txt’) }}”
> 
> - debug: msg=“{{ lookup(‘file’, ‘…/…/filerole/files/’ + item + ‘.txt’) }}”
> 
> with\_items: mylist

> **## roles/filerole/files/item1.txt**
> 
> Testing!

This produces the following output on Ansible 1.3.2, Python 2.7.5 (Homebrew), Mac OSX 10.8.5, using the command **ansible-playbook looptest.yml -i “127.0.0.1,”** :

> PLAY [127.0.0.1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [127.0.0.1]
> 
> TASK: [debug msg=“”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [127.0.0.1] =\> (item=item1) =\> {“item”: “item1”, “msg”: “item1”}
> 
> TASK: [debug msg=“Testing”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [127.0.0.1] =\> {“item”: “”, “msg”: “Testing”}  
> ERROR: /blah/blah/blah/ansible-test/roles/filerole/files/.txt does not exist

I would expect that ERROR line to be searching for /blah/blah/blah/ansible-test/roles/filerole/files/item1.txt, not just a “.txt” file with no filename portion.

Does that help at all?

Chris

---

<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:** [October 16, 2013, 8:52pm UTC](https://forum.ansible.com/t/variable-file-path-in-lookup/13688/5 "2013-10-16T20:52:39Z")

</div>

Please file a github ticket.

Thanks!

---

<div class="post-metadata">

**Author:** ![Chris\_Carter](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Chris\_Carter](https://forum.ansible.com/u/Chris_Carter)\
**Post date:** [October 16, 2013, 9:32pm UTC](https://forum.ansible.com/t/variable-file-path-in-lookup/13688/6 "2013-10-16T21:32:18Z")

</div>

You bet, [ticket 4549](https://github.com/ansible/ansible/issues/4549) created!
