# first\_available\_file & deprecation warnings

**URL:** <https://forum.ansible.com/t/first-available-file-deprecation-warnings/15231>\
**Category:** Ansible Project\
**Created:** [May 14, 2014, 11:42am UTC](https://forum.ansible.com/t/first-available-file-deprecation-warnings/15231 "2014-05-14T11:42:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bas\_Meijer](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@Bas\_Meijer](https://forum.ansible.com/u/Bas_Meijer)\
**Post date:** [May 14, 2014, 11:42am UTC](https://forum.ansible.com/t/first-available-file-deprecation-warnings/15231/1 "2014-05-14T11:42:50Z")

</div>

I cannot find documentation needed to clean up before upgrading to 1.6. Searching 'first\_available\_file' on [http://docs.ansible.com](http://docs.ansible.com), shows 4 results, but the pages themselves do not contain the search term.

PROBLEM: In the playbook below, Ansible 1.4.5 shows 'DEPRECATION WARNING' when I use $item, and 'undefined variables' when in use {{item}}.

&nbsp;&nbsp;- name: build and write /etc/hosts file  
&nbsp;&nbsp;&nbsp;&nbsp;action: template src=templates/$item dest=/etc/hosts owner=root group=root mode=0644  
&nbsp;&nbsp;&nbsp;&nbsp;first\_available\_file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- templates/etc-hosts.{{ansible\_hostname}}.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- templates/etc-hosts.j2

[DEPRECATION WARNING]: Legacy variable substitution, such as using ${foo} or  
$foo instead of {{ foo }} is currently valid but will be phased out and has  
been out of favor since version 1.2. This is the last of legacy features on our  
deprecation list. You may continue to use this if you have specific needs for  
now. This feature will be removed in version 1.6. Deprecation warnings can be  
disabled by setting deprecation\_warnings=False in ansible.cfg.

How do I refactor this?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 14, 2014, 12:01pm UTC](https://forum.ansible.com/t/first-available-file-deprecation-warnings/15231/2 "2014-05-14T12:01:57Z")

</div>

​  
​  
{{item}} is the right syntax

undefined variables is another problem, is it possible you get no match  
for first\_available\_file?

You might want to use with\_first\_found instead of the  
older first\_available\_file though, then if no match, the task would get  
skipped.

&nbsp;&nbsp;Serge

---

<div class="post-metadata">

**Author:** ![Bas\_Meijer](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@Bas\_Meijer](https://forum.ansible.com/u/Bas_Meijer)\
**Post date:** [May 14, 2014, 12:15pm UTC](https://forum.ansible.com/t/first-available-file-deprecation-warnings/15231/3 "2014-05-14T12:15:31Z")

</div>

Sililar to the example on [http://docs.ansible.com/playbooks\_loops.html](http://docs.ansible.com/playbooks_loops.html)

- name: build and write /etc/hosts file  
action: template src=templates/{{ item }} dest=/etc/hosts owner=root group=root mode=0644  
with\_first\_found:
- “etc-hosts.{{ansible\_hostname}}.j2”
- “etc-hosts.j2”

Ansible 1.4.5 fails with the newer ‘with\_first\_found’  
fatal: [backend] =\> {‘msg’: ‘One or more items failed.’, ‘failed’: True, ‘changed’: False, ‘results’: [{‘msg’: ‘unable to read /home/bas/code/iwelcome-ansible/playbooks/hosts/templates/None’, ‘failed’: True}]}

When I replace ‘with\_first\_found’ with ‘first\_available\_file’ it fails in a different way:  
fatal: [backend] =\> One or more undefined variables: ‘item’ is undefined

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 14, 2014, 12:26pm UTC](https://forum.ansible.com/t/first-available-file-deprecation-warnings/15231/4 "2014-05-14T12:26:43Z")

</div>

Yep, first\_available\_file is quite deprecated, use “with\_first\_found” instead, which is the replacement in all recent versions of Ansible.

That’s probably why you aren’t seeing any references to it. I’m not sure what was going on with search.

---

<div class="post-metadata">

**Author:** ![Bas\_Meijer](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@Bas\_Meijer](https://forum.ansible.com/u/Bas_Meijer)\
**Post date:** [May 14, 2014, 1:43pm UTC](https://forum.ansible.com/t/first-available-file-deprecation-warnings/15231/5 "2014-05-14T13:43:54Z")

</div>

Since which version has that been deprecated and when did the behaviour change? I’m running 1.4.5 and cannot get rid of the deprecation warnings because “with\_first\_found”, in 1.4.5, cannot iterate over a file not found.

Expected behaviour mentioned by Serge “then if no match, the task would get skipped.”  
Observed behaviour when there is no template named etc-hosts.backend.j2, then it crashes with:

fatal: [backend] =\> One or more undefined variables: ‘item’ is undefined

> 

If there is no alternative construct that works in 1.4.5, then there is a problem in the backward-compatibility period because the deprecation cannot be fixed before upgrade.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 14, 2014, 2:11pm UTC](https://forum.ansible.com/t/first-available-file-deprecation-warnings/15231/6 "2014-05-14T14:11:52Z")

</div>

"Since which version has that been deprecated and when did the behaviour change? I’m running 1.4.5 and cannot get rid of the deprecation warnings because “with\_first\_found”, in 1.4.5, cannot iterate over a file not found. "

I’m not sure what you mean about a behavior change, but first\_available\_file is still part of the software.

““with\_first\_found”, in 1.4.5, cannot iterate over a file not found.”

Not sure what this means, because it finds the first file it can find, and will use that one, just like the other. Can you explain more?

---

<div class="post-metadata">

**Author:** ![Bas\_Meijer](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@Bas\_Meijer](https://forum.ansible.com/u/Bas_Meijer)\
**Post date:** [May 15, 2014, 1:38pm UTC](https://forum.ansible.com/t/first-available-file-deprecation-warnings/15231/7 "2014-05-15T13:38:00Z")

</div>

It is in Ansible 1.4.4 actually, I created a repo for this that you can clone and ‘make’ on OSX with XCode, Vagrant and Virtualbox.  
The repo is 100 lines of code to provision a VM with Ansible demonstrating the issue in two commands:

git clone [git@github.com](mailto:git@github.com):datasmid/first\_available\_file.git  
make

More on [https://github.com/datasmid/first\_available\_file](https://github.com/datasmid/first_available_file)
