Force Ansible to log off to refresh user groups

Solution 1:

I am not certain if it will work for you, but I suggest you try adding a reset_connection.

- name: reset ssh connection
  meta: reset_connection

There is an example here.

  • https://docs.ansible.com/ansible/2.6/modules/meta_module.html

You may want to add this as a handler, and notify it from your user/group modification tasks. Then also add a meta: flush_handlers so the connection would only be reset if required.