Answers: <p>Answers to some parts of your question are:</p>
  
  <ul>
  <li><p>"I want to find a softlink (to another file) in the $PATH environment variable on Linux managed nodes"</p>
  
  <p>Use "shell" module instead of lookup. </p>
  
  <blockquote>
    <p><a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_lookups.html#lookups-and-variables" rel="nofollow noreferrer">Lookup</a> occur on the local computer, not on the remote computer.</p>
  </blockquote></li>
  <li><p>"I want to extract the path of the file name for the file that is found. How do I do this?"</p>
  
  <p>Use "files_found.files" to see the complete path. Checkout the return values section in <a href="https://docs.ansible.com/ansible/latest/modules/find_module.html#return-values" rel="nofollow noreferrer">find</a> module documentation.</p></li>
  <li><p>As far as i understood, you want to do something like this</p></li>
  </ul>
  
  <p>Try out the following:</p>
  
  <pre><code>- name: finding files matching "goodfile" pattern
    find:
       paths: "{{ some_path }}"
       patterns: "goodfile"
       file_type: link
    register: files_found
  
  - name : get the complete path
    set_fact:
      files_found_path: "{{ files_found.files }}"
  
   - debug:
       var: files_found_path
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2018-12-16T06:30:45.370Z
Answers: 2018-12-16T06:46:32.877Z
Answers: 2018-12-16T06:46:32.877Z
Answers: dheerendra
Body: <p>This has been reworded, expanded upon and clarified via an update late on 11/9/18.  I request it be upvoted now that it has enough code.</p>
  
  <p>I want to find a softlink (to another file) in the $PATH environment variable on Linux managed nodes.</p>
  
  <p>I have an Ansible playbook that uses a find module.  I have a string of potential directories to look in.  This string is the value assigned to the variable "result_from_prev".  My playbook looks like this:</p>
  
  <pre><code> - debug:
       msg: "{{ item }}: {{ lookup('env', 'PATH') }}"
     with_dict: {a, 1}
     register: prevar
   - set_fact:
       midvar: "{{ prevar | dictsort | join('') }}"
   - set_fact:
       result_from_prev: "{{ midvar.split(':') }}"
   - find:
       paths: "{{ item }}"
       patterns: "goodfile"
       file_type: link
     register: files_found
     loop: "{{ result_from_prev }}"
   - set_fact: 
       jsonvar: "{{ files_found | to_json }}"
   - set_fact: 
       var: "{{ files_found | list }}"
   - debug:
       msg: "{{ item }}"
     loop: "{{ files_found | json_query('files[*].path' }}"
  </code></pre>
  
  <p>I want to extract the path of the file name for the file that is found.  How do I do this?</p>
  
  <p>The above gives me an error about invalid data being passed to loop.  A list must be passed. I think that files_found is a dictionary.  How would I use wantlist=True here?  I tried, but I had no luck.</p>
  
  <p>I tried using jsonvar instead of "files_found" in the loop.  But I got the same error about invalid data being passed to 'loop.'</p>
  
  <p>I tried using listvar instead of files_found.  But I got the same error about invalid data being passed to 'loop.'</p>
  
  <p>I tried using "with_items" instead of "loop".  But this prints out nothing for the msg.</p>
  
  <p>In the JSON that the jsonvar variable can print out, there is a "matched" key.  The value is "0" for each directory path that the file was not found and the value is "1" if the file was found.  But I do not know how to extract just the full path of the file found.  I'd like it to be clean like this: </p>
  
  <blockquote>
    <p>/tmp/subdirectory/goodfile</p>
  </blockquote>
  
  <p>The potential locations of where the file will be will change.  I have a framework that works: I have the potential files in a list in a variable named "result_from_prev".</p>
  
  <p>I have tried using the "when" conditional and the item.matched notation to analyze the value of specific keys.  These efforts do not work for me.  I have tried putting the JSON into a file or using the "join" feature to create a long string.  But I have not had luck parsing this long string.  I have also tried using the "lookup" keyword with set_fact to assign a variable to that directory path that was found with the find module.  I tried to start over completely with the stat module instead of the find module.  But I found this to be difficult to use a list of potential paths whereas the searching by the file's name is supported with the find module.  I want to avoid using the "shell" module.  Nothing I have tried works.  </p>
  
  <p>I expect the find module to return only successfully found files with a supported syntax that makes extracting the directory path relatively easy.  This is not what I have experienced.  How do I return just the full path of the file named "goodfile" that is matched with the find module above?</p>
  
Comments: CC BY-SA 4.0
Comments: 2018-11-09T19:36:45.093Z
Comments: I down-rated the question because it does not [Include just enough code to allow others to reproduce the problem](https://stackoverflow.com/help/how-to-ask)
Comments: Vladimir Botka
Comments: CC BY-SA 4.0
Comments: 2018-11-10T02:20:29.823Z
Comments: Can it be up-rated now?
Comments: Ranjit
Comments: CC BY-SA 4.0
Comments: 2018-11-12T16:40:40.037Z
Comments: What could be done to improve this?
Comments: Ranjit
Tags: <ansible><linux>
Title: How do I return just the full path of a file found with the find module in Ansible?
UserDisplayName: Ranjit
ThreadText: # How do I return just the full path of a file found with the find module in Ansible?
  
  (Asked by: username_0 on 2018-11-09)
  
  This has been reworded, expanded upon and clarified via an update late on 11/9/18. I request it be upvoted now that it has enough code.
  
  I want to find a softlink (to another file) in the $PATH environment variable on Linux managed nodes.
  
  I have an Ansible playbook that uses a find module. I have a string of potential directories to look in. This string is the value assigned to the variable "result\_from\_prev". My playbook looks like this:
  
  ```
   - debug:
       msg: "{{ item }}: {{ lookup('env', 'PATH') }}"
     with_dict: {a, 1}
     register: prevar
   - set_fact:
       midvar: "{{ prevar | dictsort | join('') }}"
   - set_fact:
       result_from_prev: "{{ midvar.split(':') }}"
   - find:
       paths: "{{ item }}"
       patterns: "goodfile"
       file_type: link
     register: files_found
     loop: "{{ result_from_prev }}"
   - set_fact: 
       jsonvar: "{{ files_found | to_json }}"
   - set_fact: 
       var: "{{ files_found | list }}"
   - debug:
       msg: "{{ item }}"
     loop: "{{ files_found | json_query('files[*].path' }}"
  
  ```
  
  I want to extract the path of the file name for the file that is found. How do I do this?
  
  The above gives me an error about invalid data being passed to loop. A list must be passed. I think that files\_found is a dictionary. How would I use wantlist=True here? I tried, but I had no luck.
  
  I tried using jsonvar instead of "files\_found" in the loop. But I got the same error about invalid data being passed to 'loop.'
  
  I tried using listvar instead of files\_found. But I got the same error about invalid data being passed to 'loop.'
  
  I tried using "with\_items" instead of "loop". But this prints out nothing for the msg.
  
  In the JSON that the jsonvar variable can print out, there is a "matched" key. The value is "0" for each directory path that the file was not found and the value is "1" if the file was found. But I do not know how to extract just the full path of the file found. I'd like it to be clean like this:
  
  > /tmp/subdirectory/goodfile
  
  The potential locations of where the file will be will change. I have a framework that works: I have the potential files in a list in a variable named "result\_from\_prev".
  
  I have tried using the "when" conditional and the item.matched notation to analyze the value of specific keys. These efforts do not work for me. I have tried putting the JSON into a file or using the "join" feature to create a long string. But I have not had luck parsing this long string. I have also tried using the "lookup" keyword with set\_fact to assign a variable to that directory path that was found with the find module. I tried to start over completely with the stat module instead of the find module. But I found this to be difficult to use a list of potential paths whereas the searching by the file's name is supported with the find module. I want to avoid using the "shell" module. Nothing I have tried works.
  
  I expect the find module to return only successfully found files with a supported syntax that makes extracting the directory path relatively easy. This is not what I have experienced. How do I return just the full path of the file named "goodfile" that is matched with the find module above?
  
  **Tags:** ansible, linux
  
  > **Comments:**
  > **username_1**: I down-rated the question because it does not [Include just enough code to allow others to reproduce the problem](https://stackoverflow.com/help/how-to-ask)  
  > **username_0**: Can it be up-rated now?  
  > **username_0**: What could be done to improve this?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-12-16. Score: 1
  
  Answers to some parts of your question are:
  
  * "I want to find a softlink (to another file) in the $PATH environment variable on Linux managed nodes"
  
    Use "shell" module instead of lookup.
  
    > [Lookup](https://docs.ansible.com/ansible/latest/user_guide/playbooks_lookups.html#lookups-and-variables) occur on the local computer, not on the remote computer.
  * "I want to extract the path of the file name for the file that is found. How do I do this?"
  
    Use "files\_found.files" to see the complete path. Checkout the return values section in [find](https://docs.ansible.com/ansible/latest/modules/find_module.html#return-values) module documentation.
  * As far as i understood, you want to do something like this
  
  Try out the following:
  
  ```
  - name: finding files matching "goodfile" pattern
    find:
       paths: "{{ some_path }}"
       patterns: "goodfile"
       file_type: link
    register: files_found
  
  - name : get the complete path
    set_fact:
      files_found_path: "{{ files_found.files }}"
  
   - debug:
       var: files_found_path
  
  ```
  
  
  ---
  
  