How do I return just the full path of a file found with the find module in Ansible?

HOW TO -️ October 18, 2021

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:

ERROR Rendering Code Block

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?

Answer

I down-rated the question because it does not Include just enough code to allow others to reproduce the problem Can it be up-rated now? What could be done to improve this?

Initializing...