# first\_available\_file not providing its results to item variable?

**URL:** <https://forum.ansible.com/t/first-available-file-not-providing-its-results-to-item-variable/13499>\
**Category:** Ansible Project\
**Created:** [September 18, 2013, 3:16am UTC](https://forum.ansible.com/t/first-available-file-not-providing-its-results-to-item-variable/13499 "2013-09-18T03:16:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javier\_Candeira](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@Javier\_Candeira](https://forum.ansible.com/u/Javier_Candeira)\
**Post date:** [September 18, 2013, 3:16am UTC](https://forum.ansible.com/t/first-available-file-not-providing-its-results-to-item-variable/13499/1 "2013-09-18T03:16:30Z")

</div>

I'm having trouble with first-available file, which I'm using to  
authorise a ssh key for a given user like so:

&nbsp;&nbsp;# TODO: add a forced-command so only gitpulling is allowed  
&nbsp;&nbsp;- name: put coreapi public key in cms user XXXX for synching  
&nbsp;&nbsp;&nbsp;&nbsp;authorized\_key: user=XXXX state=present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key=$FILE({{item}})  
&nbsp;&nbsp;&nbsp;&nbsp;first\_available\_file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- keys/whitelabel\_sites/{{deploy}}.coreapi.{{customer}}.enso\_id\_rsa.pub  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- keys/javier

This is my debug playbook:

&nbsp;&nbsp;- name: find out why the key isn't getting proper authorised 0 --  
the variable interpolation  
&nbsp;&nbsp;&nbsp;&nbsp;debug: msg="keys/whitelabel\_sites/{{deploy}}.coreapi.{{customer}}.enso\_id\_rsa.pub"

&nbsp;&nbsp;- name: find out why the key isn't getting proper authorised 1 -- the filename  
&nbsp;&nbsp;&nbsp;&nbsp;debug: msg=$item  
&nbsp;&nbsp;&nbsp;&nbsp;first\_available\_file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- keys/whitelabel\_sites/{{deploy}}.coreapi.{{customer}}.enso\_id\_rsa.pub  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- keys/javier

&nbsp;&nbsp;- name: find out why the key isn't getting proper authorised 2 --  
the file contents  
&nbsp;&nbsp;&nbsp;&nbsp;debug: msg=$FILE({{item}})  
&nbsp;&nbsp;&nbsp;&nbsp;first\_available\_file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- keys/whitelabel\_sites/{{deploy}}.coreapi.{{customer}}.enso\_id\_rsa.pub  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- keys/javier

And this is the output: only the customer name has been changed to  
"customername":

TASK: [find out why the key isn't getting proper authorised 0 -- the  
variable interpolation] \*\*\*\*  
ok: [a.cms.customername.artpro.co] =\> {"msg":  
"keys/whitelabel\_sites/a.coreapi.customername.enso\_id\_rsa.pub"}

TASK: [find out why the key isn't getting proper authorised 1 -- the  
filename] \*\*\*  
ok: [a.cms.customername.artpro.co] =\> {"msg": "$item"}

TASK: [find out why the key isn't getting proper authorised 2 -- the  
file contents] \*\*\*  
fatal: [a.cms.customername.artpro.co] =\> One or more undefined  
variables: 'item' is undefined

FATAL: all hosts have already failed -- aborting

Both files keys/whitelabel\_sites/a.coreapi.customername.enso\_id\_rsa.pub  
and keys/javier exist, and can be accessed. In fact one of them is  
created in the previous play, with the same variable templating.

I'm about to declare it a bug in ansible, and not in my playbook. If  
anybody can either confirm or disabuse me, I'll appreciate it.

Thanks,

Javier

---

<div class="post-metadata">

**Author:** ![Javier\_Candeira](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@Javier\_Candeira](https://forum.ansible.com/u/Javier_Candeira)\
**Post date:** [September 18, 2013, 3:17am UTC](https://forum.ansible.com/t/first-available-file-not-providing-its-results-to-item-variable/13499/2 "2013-09-18T03:17:52Z")

</div>

Sorry for replying to myself. I just upgraded to tag v1.3.1. This is  
where the erroneous output comes from.

J

---

<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:** [September 18, 2013, 12:12pm UTC](https://forum.ansible.com/t/first-available-file-not-providing-its-results-to-item-variable/13499/3 "2013-09-18T12:12:29Z")

</div>

So one possible thing I’d like you to change and then let us know if you still see a problem:

$FILE({{ item }}) should be written as

{{ lookup(‘file’,item) }}

$FILE is legacy syntax, and also mixing old style templating and new is generally a bad idea as one runs before each other.

Let me know if you still have a problem after changing things over.

---

<div class="post-metadata">

**Author:** ![Javier\_Candeira](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@Javier\_Candeira](https://forum.ansible.com/u/Javier_Candeira)\
**Post date:** [September 20, 2013, 2:53am UTC](https://forum.ansible.com/t/first-available-file-not-providing-its-results-to-item-variable/13499/4 "2013-09-20T02:53:40Z")

</div>

Hi, Michael.

Thanks for your answer. The mucking about with $item and {{item}} was  
part of my attempts at debugging. I had actually started interpolating  
all variables in the jinja style. Thanks also for the advice on the  
deprecation of $FILE syntax.

The issue was solved by benno on IRC. I repeat his solution here for  
reference. It turns out that first\_available\_file is an option for the  
copy and template modules, but not for the authorize\_key module. His  
solution was to use with\_first\_found, which is universally available  
as a lookup plugin since [https://github.com/ansible/ansible/pull/2260](https://github.com/ansible/ansible/pull/2260).

I see this as a documentation bug, and will be providing a patch to  
the advanced playbooks docs page over the weekend.

However, the documentation might need a bit more significant overhaul.  
May I suggest two more pages?

- leave a cursory explanation of lookup plugins in the advanced  
playbooks page, and make a page with an exhaustive reference for types  
of operators/lookups/etc, perhaps working from of  
[https://groups.google.com/forum/#!topic/ansible-project/IVUwp9195Ek](https://groups.google.com/forum/#!topic/ansible-project/IVUwp9195Ek)  
- make a specific page for the evolution of syntax/modules, so people  
who have old playbooks don't have to look all around the documentation  
for their needed changes. This is especially important for those of us  
wearing several hats, and coming back to Ansible maybe once weekly, or  
less frequently.

Regards,

Javier

---

<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:** [September 21, 2013, 1:02pm UTC](https://forum.ansible.com/t/first-available-file-not-providing-its-results-to-item-variable/13499/5 "2013-09-21T13:02:44Z")

</div>

with\_first\_found is the new alternative to “first\_available\_file”.

You should only see FAF in legacy documentation and old examples.

The idea was to generalize it to a lookup plugin.

Right now you can keep up with the changelog, but the documentation will always show the favored way to do everything.

I don’t want to full up the docs with cruft about old systems, but we take lots of strides to ensure backwards compat on things.

If you try something that doesn’t have a docs example, expect it to not be a thing 🙂
