Trying to get the $? in the Ansible context you show us, to have the state of success of the last command given to Ansible to execute remotely won't work.
The $? variable is for returning the status of the last command run in the current shell.
In the setup you are showing us you neither have a guarantee the last running command was the one shown in Ansible, nor it is the same shell, and possibly, depending on your ssh client configuration, not even the same ssh session.
Per the example in the Ansible official guide: to do something else if the last executed task/command was successful or not, you do:
- command: /bin/false
- command: /bin/something
when: result is failed
- command: /bin/something_else
when: result is succeeded
For further details, see Documentation->Ansible->Conditionals