So You want to know the O.S name and version via Ansible
Without Wasting Around
BEFORE YOU GO THROUGH THIS READ ABOUT ANSIBLE FACT
Create 2 Files by the following Command
echo "package: httpd" > RedHat-8.yml
echo "package: apache2" > Ubuntu-18.yml
Then Write The Following Play-Book You can customize the above files as you wish but here I'm going through this for simplicity
After That Run the PlayBook with
ansible-playbook NAME_OF_PLAYBOOK.yml
And There You Go You installed the Web-Server According to the OS (RedHat/Centos or Ubuntu)
with their respective package manager
Thanks For Reading