I’m not knowledgeable enough about Python ecosystem to give you an appropriate answer, but I’d suggest to try the upgrade process using venvs (or virtualenvs for Python 2.7). Using python-based image containers would be another viable option.
Start with a venv for your current Python version, install Ansible into it (again, current version), then try upgrading Python in your venv, though I’m not sure you can upgrade a virtualenv (python 2.x) into a venv (python 3.x), so you might have to do it incrementally and rebuild as needed. Using containers might be easier for this purpose.
Please tell if you need assistance on any of these options !
What @ptn told you is correct, but if you need more details about how to use different python versions on your system without messing things-up, this other topic may be of help to you: