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/reference_appendices/special_variables.html :
group_names is a list (array) of all the groups the current host is in.