Here's another way to do this:
- name: my command
command: echo stuff
when: "'groupname' not in group_names"
group_names
is a magic variable as documented here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-information-about-other-hosts-with-magic-variables :
group_names is a list (array) of all the groups the current host is in.