How to get only stdout_lines with Ansible using mail module
0
votes
1
answer
2135
views
I'm using ansible to scan hosts with clam antivirus and I'm receiving reports via email.
Reports contain everything and I want only to receive stdout lines.
Is there any way to accomplish this?
I wrote email template according to https://gist.github.com/halberom/0aea275632d2b47af0536e5def01d4d2
although only thing that is not the same is that I've piped to nice json:
The {{ host }} says {{ hostvars[host]['result']['stdout'] | to_nice_json }}
The error I get is:
> Unable to convert data using to_nice_json, falling back to to_json: 'dict object' has no attribute 'stdout'. the task includes an option with an undefined variable the error was ansible no_log:false
If I remove
['stdout']
then email report looks like this:
ClamAV scan was performed on host_server
{
"changed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": [
"clamscan",
"-r",
"-i",
"/usr/bin"
],
"delta": "0:00:37.293719",
"end": "2021-09-09 18:47:55.626094",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "clamscan -r -i /usr/bin",
"_uses_shell": false,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"item": "/usr/bin",
"rc": 0,
"start": "2021-09-09 18:47:18.332375",
"stderr": "",
"stderr_lines": [],
"stdout": "\n----------- SCAN SUMMARY -----------\nKnown viruses: 8563204\nEngine version: 0.103.2\nScanned directories: 1\nScanned files: 701\nInfected files: 0\nData scanned: 110.08 MB\nData read: 109.50 MB (ratio 1.01:1)\nTime: 37.113 sec (0 m 37 s)\nStart Date: 2021:09:09 18:47:18\nEnd Date: 2021:09:09 18:47:55",
"stdout_lines": [
"",
"----------- SCAN SUMMARY -----------",
"Known viruses: 8563204",
"Engine version: 0.103.2",
"Scanned directories: 1",
"Scanned files: 701",
"Infected files: 0",
"Data scanned: 110.08 MB",
"Data read: 109.50 MB (ratio 1.01:1)",
"Time: 37.113 sec (0 m 37 s)",
"Start Date: 2021:09:09 18:47:18",
"End Date: 2021:09:09 18:47:55 "
]
}
]
}
Is there a way to accomplish getting only stdout_lines in my report? I tried with grep in the shell command but failed
Asked by shiva
(3 rep)
Sep 9, 2021, 03:26 PM
Last activity: May 13, 2025, 04:02 PM
Last activity: May 13, 2025, 04:02 PM