How to make emacs detect the correct file type when the extension is the same? (Ansible vs YAML, etc.)

If I open an Ansible playbook or role, although it’s in the yaml mode, ansible-mode does not get enabled automatically. I was wondering if there was some way to detect that it’s an ansible file using heuristics on the contents of the file.