# file ... with\_first\_found uses unexpected value

**URL:** <https://forum.ansible.com/t/file-with-first-found-uses-unexpected-value/27125>\
**Category:** Ansible Project\
**Created:** [March 27, 2018, 9:44am UTC](https://forum.ansible.com/t/file-with-first-found-uses-unexpected-value/27125 "2018-03-27T09:44:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Werner\_Flamme](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@Werner\_Flamme](https://forum.ansible.com/u/Werner_Flamme)\
**Post date:** [March 27, 2018, 9:44am UTC](https://forum.ansible.com/t/file-with-first-found-uses-unexpected-value/27125/1 "2018-03-27T09:44:53Z")

</div>

Hi all,

I have a piece of code that gives an unexpected error:

--- snip ---  
- name: link the proper certificate issue directory  
&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;src: '{{ item }}'  
&nbsp;&nbsp;&nbsp;&nbsp;dest: '/etc/mycert/current'  
&nbsp;&nbsp;&nbsp;&nbsp;state: link  
&nbsp;&nbsp;&nbsp;&nbsp;follow: yes  
&nbsp;&nbsp;with\_first\_found:  
&nbsp;&nbsp;&nbsp;&nbsp;- "/etc/mycert/von\_2020"  
&nbsp;&nbsp;&nbsp;&nbsp;- "/etc/mycert/von\_2019"  
&nbsp;&nbsp;&nbsp;&nbsp;- "/etc/mycert/von\_2018"  
&nbsp;&nbsp;&nbsp;&nbsp;- "/etc/mycert/von\_2017"  
&nbsp;&nbsp;&nbsp;&nbsp;- "/etc/mycert/von\_2016"  
&nbsp;&nbsp;&nbsp;&nbsp;- "/etc/mycert/von\_2015"  
&nbsp;&nbsp;&nbsp;&nbsp;- "/etc/mycert/von\_2014\_09"  
&nbsp;&nbsp;&nbsp;&nbsp;- "/etc/mycert/von\_2014"  
&nbsp;&nbsp;when: ( facter\_mycertdir != '/etc/mycert/current' )  
--- pins ---

When I look at /etc/mycert/, I see a subdirectory "von\_2018", and I  
expect the file module to create a symlink from "/etc/mycert/von\_2018"  
to '/etc/mycert/current'. Instead, I get an error:

--- snip ---  
TASK [my.certs : link the proper certificate issue directory]

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 27, 2018, 10:17am UTC](https://forum.ansible.com/t/file-with-first-found-uses-unexpected-value/27125/2 "2018-03-27T10:17:00Z")

</div>

with\_first\_found always do lookup on localhost not the remote host.  
To get files on remote you need to use the find module.

---

<div class="post-metadata">

**Author:** ![Werner\_Flamme](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@Werner\_Flamme](https://forum.ansible.com/u/Werner_Flamme)\
**Post date:** [March 27, 2018, 11:09am UTC](https://forum.ansible.com/t/file-with-first-found-uses-unexpected-value/27125/3 "2018-03-27T11:09:23Z")

</div>

Kai Stian Olstad [27.03.2018 12:17]:

> > Hi all,
> > 
> > I have a piece of code that gives an unexpected error:
> > 
> > --- snip ---  
> > - name: link the proper certificate issue directory  
> > &nbsp;&nbsp;file:  
> > &nbsp;&nbsp;&nbsp;&nbsp;src: '{{ item }}'  
> > &nbsp;&nbsp;&nbsp;&nbsp;dest: '/etc/mycert/current'  
> > &nbsp;&nbsp;&nbsp;&nbsp;state: link  
> > &nbsp;&nbsp;&nbsp;&nbsp;follow: yes  
> > &nbsp;&nbsp;with\_first\_found:  
> > &nbsp;&nbsp;&nbsp;&nbsp;- "/etc/mycert/von\_2020"  
> > &nbsp;&nbsp;&nbsp;&nbsp;- "/etc/mycert/von\_2019"

[...]

> > The "von\_2016" would be correct on the host I use as ansible master,  
> > but not on the host (sapvhrq) where the play is executed.
> > 
> > What do I do wrong here?
> 
> with\_first\_found always do lookup on localhost not the remote host.  
> To get files on remote you need to use the find module.

Oh, thanks, I didn't see this mentioned on the  
\<[http://docs.ansible.com/ansible/latest/plugins/lookup/first\_found.html&gt](http://docs.ansible.com/ansible/latest/plugins/lookup/first_found.html&gt);  
page.

I see how I can use the find module. But how can I get the "latest"  
directory from the find module?

Do I have to split into several "stat" statements, one per year,  
beginning with 2020? Plus one "file" statement for each stat, to create  
the desired symlink?

Since the "/etc/mycert" directory is a symlink too, pointing to a shared  
filesystem, I try to use other variable, it works here, but not all of  
my hosts can share this filesystem.

As dest, I use "/clusterdisk/sapdirs/{{ facter\_myhostname }}/current",  
and the with\_first\_found list now reads like

&nbsp;&nbsp;&nbsp;&nbsp;- "/clusterdisk/sapdirs/{{ facter\_myhostname }}/von\_2020"  
&nbsp;&nbsp;&nbsp;&nbsp;- "/clusterdisk/sapdirs/{{ facter\_myhostname }}/von\_2019"

Regards,  
Werner

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 27, 2018, 12:28pm UTC](https://forum.ansible.com/t/file-with-first-found-uses-unexpected-value/27125/4 "2018-03-27T12:28:20Z")

</div>

> Kai Stian Olstad [27.03.2018 12:17]:
> 
> > with\_first\_found always do lookup on localhost not the remote host.  
> > To get files on remote you need to use the find module.
> 
> Oh, thanks, I didn't see this mentioned on the  
> \<[http://docs.ansible.com/ansible/latest/plugins/lookup/first\_found.html&gt](http://docs.ansible.com/ansible/latest/plugins/lookup/first_found.html&gt);  
> page.

This apply to all lookups and template so it documented on main page  
[http://docs.ansible.com/ansible/latest/plugins/lookup.html](http://docs.ansible.com/ansible/latest/plugins/lookup.html)  
"Like all templating, these plugins are evaluated on the Ansible control machine, not on the target/remote."

> I see how I can use the find module. But how can I get the "latest"  
> directory from the find module?
> 
> Do I have to split into several "stat" statements, one per year,  
> beginning with 2020? Plus one "file" statement for each stat, to create  
> the desired symlink?

Since you can sort on name this will find newest entry

&nbsp;&nbsp;&nbsp;- find:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths: /tmp  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: r

&nbsp;&nbsp;&nbsp;- debug: msg="{{ r.files | map(attribute='path') | list | sort | last }}"

> Since the "/etc/mycert" directory is a symlink too, pointing to a shared  
> filesystem, I try to use other variable, it works here, but not all of  
> my hosts can share this filesystem.

I think you can use find module on a symlink.
