Ahhh you are marrying to apt here. There is a way to do this programmatically while not depending on the package manager or the package name. 

See this as an example implementation: https://github.com/ansible/ansible-examples/blob/master/language_features/conditionals_part1.yml

Basically, depending on the distro you will require a playbook for Debian and another for CentOS (or any other if they are added/supported)