1

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.

2
  • I don't think there's a great way to do this. Package naming is going to vary across distributions and across distribution versions. Probably easier to maintain a static mapping of distribution and version to appropriate pip package name, or better yet just document the requirement external to your playbooks. – larsks Dec 14 '19 at 14:04
  • @larsks I only care about Debian derivatives, so concerning the naming across distros I think it'll be fine. But thanks a lot for the input. I'll stick with the way I am doing it right now and add some assertions for good measure. Perhaps there will be an answer in the future, so I'll defer to the community to close and/or delete it. – 0xC0000022L Dec 14 '19 at 21:35
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.