How to determine which packaged pip to install in order to run the Ansible pip module? I am struggling to find a reasonable method to detect which of python-pip or python3-pip I should install depending on the contents of the ansible_python fact. This is on Debian and Ubuntu.

Looking at ansible_python contents, I did not see anything that would let me correlate the Python version Ansible uses with the package name used to install that Python version. So even though I could glean the path to the Python executable from the fact I don't see how to determine the package used to install it.

The rationale is trying to avoid hardcoding stuff into my playbook. But failing everything else I'd also be willing to hardcode these assumptions into my playbook. The "best" I've come up with so far was to use the package_facts module to collect the information on installed packages, then use ansible_python.version.major to pick from either python-pip or python3-pip, which still contains the assumptions: 1.) that ansible_python.version.major==2 maps to python-pip and ansible_python.version.major==3 maps to python3-pip and 2.) that ansible_python.executable does name either of the packaged Python versions available in the python and python3 packages respectively.

Is there an objectively better way? Better isn't meant to elicit opinions here, anything getting rid of the implied assumptions qualifies as objectively better.
There are two options either you make sure you use the same Python version on each host or you make sure the right packages are installed on each platform.

Chose same Python interpreter

Provide the python binary to use. Since you have a Debian on each host, the location is always the same. If you know a Python version that is available on each host, go for this.

Either in your inventory hosts

[webserver]
srv1.example.com
srv2.example.com

[webserver:vars]
ansible_python_interpreter=/usr/bin/python3


Or in your Ansible configuration ansible.cfg

ansible_python_interpreter=/usr/bin/python3


See also https://docs.ansible.com/ansible/latest/reference_appendices/python_3_support.html#using-python-3-on-the-managed-machines-with-commands-and-playbooks

Package per Python version

If you do not know whether either Python 3 or 2 is available on each host, ensure to get the right package for each Python version.

- apt:
    name: python-pip
  when: ansible_python.version.major==2

- apt:
    name: python3-pip
  when: ansible_python.version.major==3


You may face the same issue when installing Python packages. So you can put your Pythone version specific tasks in a v2 and in a v3 file and conditionally include one for version 2 and the other for version 3.