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?