my inventory file\'s contents -
[webservers] x.x.x.x ansible_ssh_user=ubuntu [dbservers] x.x.x.x ansible_ssh_user=ubuntu
in my tasks file
If you want to run your role on all hosts but only a single task limited to the webservers group, then - like you already suggested - when is your friend.
webservers
You could define a condition like:
when: inventory_hostname in groups['webservers']