I imagine the problem is that ansible_hostname is the portion of the fqdn. hostname before the first dot. You will likely want to look at ansible_fqdn instead.
If you run the setup module against a host, you should be able to look at all of the facts to find the appropriate fact names.
Thanks for the info Matt. I Don’t have DNS configured and hence even ‘ansible_fqdn’ is giving me random stuff. For time being I’ve fixed it with ‘group[‘groupname’]’ to iterate over my group and do some regex matching.