# How to locate the proper file?

**URL:** <https://forum.ansible.com/t/how-to-locate-the-proper-file/21688>\
**Category:** Ansible Project\
**Created:** [June 7, 2016, 1:52pm UTC](https://forum.ansible.com/t/how-to-locate-the-proper-file/21688 "2016-06-07T13:52:04Z")\
**Posts on this page:** 2\
**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:** [June 7, 2016, 1:52pm UTC](https://forum.ansible.com/t/how-to-locate-the-proper-file/21688/1 "2016-06-07T13:52:04Z")

</div>

Hi everyone,

I have to copy a file (on the client) to a certain location (also on the  
client).

I know the copy module, but my problem is the source location. The  
filename is always the same, like foo.pem.

- if the directory "/some/dir/{{ ansible\_hostname }}" does not exist, I  
have to create a new file  
- if this directory exists, it may have a subdirectory "in\_2016". If  
this is the case, I have to take the file foo.pem from here  
- if not, there may be a subdirectory "in\_2015". If this is the case, I  
have to take the file foo.pem from here  
... plus several older directories.

A construction "with\_first\_found:" can't be applied here, can it?

Currently I try something like

=== snip ===  
- name: find proper directory (2016)  
&nbsp;&nbsp;find: paths=/some/dir/{{ ansible\_hostname }}/in\_2016 file\_type=directory  
&nbsp;&nbsp;register: dir2016  
&nbsp;&nbsp;ignore\_errors: True

- debug: var=dir2016

- name: find proper directory (2015)  
&nbsp;&nbsp;find: paths=/some/dir/{{ ansible\_hostname }}/in\_2015 file\_type=directory  
&nbsp;&nbsp;register: dir2015  
&nbsp;&nbsp;ignore\_errors: True  
&nbsp;&nbsp;when: dir2016.rc != 0

=== snip ===

But then I have to use a bazillion of copy commands containing "when:  
dir20xx == 0".

Is there a better way to get this done? I'm using ansible 2.1.0.0 on  
SLES 11 SP4.

Maybe it is best to copy a (bash) script to the host and execute it, but  
I prefer the documented way via ansible 😉

Regards,  
Werner

---

<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:** [June 9, 2016, 10:10am UTC](https://forum.ansible.com/t/how-to-locate-the-proper-file/21688/2 "2016-06-09T10:10:04Z")

</div>

Werner Flamme [07.06.2016 15:52]:

> Hi everyone,
> 
> I have to copy a file (on the client) to a certain location (also on the  
> client).
> 
> I know the copy module, but my problem is the source location. The  
> filename is always the same, like foo.pem.

[...]

JFTR:

By reading "Mastering Ansible", written by by Jesse Keating, I found out  
about the stat module, "set\_facts" and registered variables in loops...  
So I think I solved my problem now with two tasks:

=== snip ===  
&nbsp;&nbsp;- vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mydirlist:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- in\_2014  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- in\_2014\_09  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- in\_2015  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- in\_2016  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mytargetdir: 'nonsense'

&nbsp;&nbsp;- name: figuring the directory  
&nbsp;&nbsp;&nbsp;&nbsp;stat: path=/some/dir/{{ ansible\_hostname }}/{{ item }}  
&nbsp;&nbsp;&nbsp;&nbsp;register: fig1  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ mydirlist }}"

&nbsp;&nbsp;- name: note the first found path  
&nbsp;&nbsp;&nbsp;&nbsp;set\_fact: mytargetdir="{{ item.stat.path }}"  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ fig1.results }}"  
&nbsp;&nbsp;&nbsp;&nbsp;when: mytargetdir == 'nonsense' and item.stat.exists == True  
=== pins ===

Regards,  
Werner
