Ansible execute code when Host is a part of of Group X

Hello, I want to make a code that will execute on all my webserver but some part of my webserver i want to execute only on a specific machine from the webserver list ot when that machine is also part of group of [mainws] or [secondaryws]

`
webservers:
1.1.1.1
2.2.2.2

mainws
1.1.1.1

secondary
2.2.2.2
`

`
-host webservers

tasks:

  • name: execute task

    when: >
    host : IP or group or some varialbe

`

How can i write the when part in order to ran my task only on the mainws ?

when: inventory_hostname in groups['mainws']

The Kai answer was perfect, but could be useful (may be to this problem or may be later) this page, which shows patterns for the groups definitions: https://docs.ansible.com/ansible/latest/user_guide/intro_patterns.html