I have a code as below and i have my html files present in /tmp/sanity/ .
but i am getting error as “Failed to send mail: can’t attach file” i get file not found
name: list html files
run_once: true
local_action: shell find /tmp/sanity/ -type f -regex “.*.html” | tr -s ‘\n’ ’ ’ | xargs | sort
register: list_html_files
debug: msg=“{{ list_html_files }}”
name: generate final email body
run_once: true
local_action: shell for i in find /tmp/sanity/ -type f -regex ".*email.*.txt" | sort; do cat $i ;done
register: email_body
I have a code as below and i have my html files present in /tmp/sanity/ .
but i am getting error as "Failed to send mail: can't attach file" i get file not found
Well that's pretty clear: the file is not found.
Debug what is in list_html_files.stdout_lines[0] and adjust that.
Without any further information it's hard to tell.
On a related note, instead of fragile shell find/tr/xargs/sort , I
would strongly suggest to use the 'find' module.
That will at least return a standard data structure (which is easier
to offer help with).
The problem is not searching the file in the location. the problem is “attach” inside mail module doesn’t recognize the variable reference(it takes hard coded path).I dont want to hard code path inside attach. I want to use the variable reference inside attach module.
In the debug of “list_html_files” below it clearly shows that the html files are present. i am using ansible.2.7.5
You deliberately replaced newlines with spaces, so now your creative
find/tr/xargs/sort command yields only one (1) line.
Hence the attach parameter will see one line that isn't a file, and fail.
I will repeat my previous advice: drop the shell and use the find module.
The documentation is here: https://docs.ansible.com/ansible/latest/modules/find_module.html