Manage Windows like Linux with Ansible?

This should work but changes the systems python3.

If possible use a venv, this way systems python stays untouched and only pip in the virtual environment is updated.

# Create venv
python3 -m venv /opt/ansible
# Enter venv
source /opt/ansible/bin/activate

Example of updating pip in a virtualenv:

[root@testserver ~]# python -V
Python 2.7.5

[root@testserver ~]# python3 -V
Python 3.6.8

[root@testserver ~]# python3 -m pip -V
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

[root@testserver ~]# source /opt/ansible/bin/activate
(ansible) [root@testserver ~]# python -V
Python 3.6.8

(ansible) [root@testserver ~]# pip -V
pip 9.0.3 from /opt/ansible/lib64/python3.6/site-packages (python 3.6)

(ansible) [root@testserver ~]# pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/ca/31/b88ef447d595963c01060998cb329251648acf4a067721b0452c45527eb8/pip-21.2.4-py3-none-any.whl (1.6MB)
    100% |████████████████████████████████| 1.6MB 885kB/s
Installing collected packages: pip
  Found existing installation: pip 9.0.3
    Uninstalling pip-9.0.3:
      Successfully uninstalled pip-9.0.3
Successfully installed pip-21.2.4

(ansible) [root@testserver ~]# pip -V
pip 21.2.4 from /opt/ansible/lib64/python3.6/site-packages/pip (python 3.6)

Another way is to use Software Collections to use more recent versions of python. @stephdl provides different scl python versions with additional options.

Here is an example of using rh-python38:

yum -y install rh-python38
scl enable rh-python38 bash

# create virtual environment
python -m venv /opt/ansible

# enter virtual environment
source /opt/ansible/bin/activate
pip install --upgrade pip

Another example: Matrix-synapse uses rh-python36.

1 Like