Gather information from running commands

Does anyone know if it’s possible to run a command across several nodes with a playbook to gather information?
Specifically I would like to run either;
Debian / Ubuntu Based: unattended-upgrade --dry-run -d
RHEL / CentOS Based: yum list-security

and have the result outputted to the terminal ot text file. This would save logging into each server individually and checking.

You can write a cookbook to run a command on several nodes based on the OS type. After that write a Inspec file to check whether the command executed successfully or not without logging into the machines.
EX:
package ‘Install Apache’ do
case node[:platform]
when ‘redhat’, 'centos’
package_name 'httpd’
when ‘ubuntu’, 'debian’
package_name 'apache2’
end
end