Change python to use ansible

Good afternoon,

I got to python versions installed in my server and when I upgrade ansible the version I use it was never upgraded. So I discovered there are two version of python installed.

I wonder if its possible to change the version of python is using ansible right now

ansible --version
ansible [core 2.14.2]
config file = /etc/ansible/ansible.cfg
configured module search path = [‘/root/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python3.11/site-packages/ansible
ansible collection location = /root/.ansible/collections/ansible_collections:/root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.11.2 (main, Sep 12 2023, 00:00:00) [GCC 11.3.1 20221121 (Red Hat 11.3.1-4)] (/usr/bin/python3.11)
jinja version = 3.1.2
libyaml = True

Like you see is using python3.11 butn when i try to update I received this:

Requirement already satisfied: ansible in /usr/local/lib/python3.9/site-packages (8.7.0)
Requirement already satisfied: ansible-core~=2.15.7 in /usr/local/lib/python3.9/site-packages (from ansible) (2.15.8)
Requirement already satisfied: PyYAML>=5.1 in /usr/lib64/python3.9/site-packages (from ansible-core~=2.15.7->ansible) (5.4.1)
Requirement already satisfied: cryptography in /usr/lib64/python3.9/site-packages (from ansible-core~=2.15.7->ansible) (36.0.1)
Requirement already satisfied: importlib-resources<5.1,>=5.0 in /usr/local/lib/python3.9/site-packages (from ansible-core~=2.15.7->ansible) (5.0.7)
Requirement already satisfied: resolvelib<1.1.0,>=0.5.3 in /usr/lib/python3.9/site-packages (from ansible-core~=2.15.7->ansible) (0.5.4)
Requirement already satisfied: jinja2>=3.0.0 in /usr/local/lib/python3.9/site-packages (from ansible-core~=2.15.7->ansible) (3.1.2)
Requirement already satisfied: packaging in /usr/lib/python3.9/site-packages (from ansible-core~=2.15.7->ansible) (20.9)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib64/python3.9/site-packages (from jinja2>=3.0.0->ansible-core~=2.15.7->ansible) (2.1.3)
Requirement already satisfied: cffi>=1.12 in /usr/lib64/python3.9/site-packages (from cryptography->ansible-core~=2.15.7->ansible) (1.14.5)
Requirement already satisfied: pycparser in /usr/lib/python3.9/site-packages (from cffi>=1.12->cryptography->ansible-core~=2.15.7->ansible) (2.20)
Requirement already satisfied: pyparsing>=2.0.2 in /usr/lib/python3.9/site-packages (from packaging->ansible-core~=2.15.7->ansible) (2.4.7)
Requirement already satisfied: ply==3.11 in /usr/lib/python3.9/site-packages (from pycparser->cffi>=1.12->cryptography->ansible-core~=2.15.7->ansible) (3.11

Here you can see python3.9

My OS is Rocky Linux 9.2 (Blue Onyx)

thanks

Hello @frit0-rb, welcome to the Community!

What I see from the information you provide is that you already got Ansible installed system wide as root user.

When using Ansible on CLI, the common approach is to use python virtual environments. I’ll tell you what I use to do on Debian based distros (sorry about that!) but the idea would be more or less the same for RH based distros, as Rocky Linux. Also, since I use different python versions for different purposes, I’ll tell you how to switch between different ones:

1 - First, I install different python versions system wide and then I use the update-alternatives command to set the one I want to work with. Imagine that you already have Python3.8 & Python 3.9 along with the python3.x-venv packages installed on your system, but you want to have the ability to switch between those. To do so, you can proceed as follows:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
sudo update-alternatives --set python3 /usr/bin/python3.9

After the last command, you should see something like this:

update-alternatives: using /usr/bin/python3.9 to provide /usr/bin/python3 (python3) in manual mode

If you check the python version you will get notice of the one you’re currently on:

python3 --version
Python 3.9.18

2 - Now you need to create a virtual environment. Here is where you will be installing your ansible-core pip package:

python -m venv ansible_venv
source ansible_venv/bin/activate
pip install ansible-core==2.15.6

3 - Finally, if you run ansible --version with your venv enabled, you should see that you’re on the Python version you selected on step 1:

ansible [core 2.15.6]
  config file = None
  configured module search path = ['/home/beri/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/beri/.local/lib/python3.9/site-packages/ansible
  ansible collection location = /home/beri/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/beri/.local/bin/ansible
  python version = 3.9.18 (main, Aug 25 2023, 13:20:14) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = False

Be aware that there are different ways to do that. If other forum users want to tell what their approaches are, they will be very welcome indeed :slight_smile:

Hope it helps!

2 Likes

First of all, thanks for your time.

I understand what you are trying to recommend me, but I’m a litte bit new with ansible.

First of all, If I create a new venv, what thats means?

Second, If I install a new ansible-core, all inventory for the actual is it necessary to recreate or how?

One more thing, Im using pip3 and when I follow another instructions to run python3.11 to update ansible I received a message like this: /usr/bin/python3.11: No module named pip

so in this case that module isn´t exist in my python3.11 folder

1 Like

Ok Roberto, I see… I’ll try to slow down a little.

This means that you’ll have a dedicated Python environment which will be totally isolated from your OS, where you’ll be able to install a set of Python packages. This virtual environment could be useful to run specific Python applications and its related dependencies. Also, you can have as many venvs as you may need. For example, if you need to run ansible-core==2.9 for legacy ansible playbooks, along with ansible-core=2.15.6 for cutting-edge ones, then you can create two different venvs for that purpose. To do so:

python -m venv ansible-2.15_venv # Creates the venv
source ansible-2.15_venv/bin/activate # Enables the venv
pip install ansible-core==2.15.6

# Do whatever you need to do with ansible, e.g.

ansible --version
ansible [core 2.15.6]
...

deactivate # Disables the venv

Later, if you wanted to use ansible==2.9 on the very same machine, you can create another venv to do so:

python -m venv ansible-2.9_venv # Creates the venv
source ansible-2.9_venv/bin/activate # Enables the venv
pip install ansible-core==2.9.x

# Do whatever you need to do with ansible 2.9, e.g.

ansible --version
ansible [core 2.9.x]
...

deactivate # Disables the venv

I suggest you to read the python venvs documentation to learn further about this feature, which I’ll tell you, will save your day more than once hehe :wink:

Not quite, the venv just affects the ansible binaries and related dependencies you got installed. The ansible code - including your inventory - will remain intact on the folder you got it stored (btw, you can enable the venv on any system directory, but you’ll have to reference its absolute path with the source command)

That’s because you need to install the python pip packager on your system. Since I’m on Debian I’m not sure of the correct command right now for RH based distros, but I can check it later if you need to:

python -m ensurepip --default-pip

Here you will find more info about how to use pip:

https://packaging.python.org/en/latest/tutorials/installing-packages/#ensure-pip-setuptools-and-wheel-are-up-to-date

PS: I just noticed you have a Hispanic name! If you feel more comfortable we can use Spanish from now on (pick your choice, I’m very used to give support in English too). Although there is not a Get Help category for the Hispanic-speaking community yet, it might be a good idea and we can ask the admins to create it :wink: What do you think @gwmngilfen @Leo ?

3 Likes

Muchas gracias por todo Jordi, como bien dices soy Español.

Lo he conseguido solucionar instalando el modulo de pip dentro python3.11con el siguiente comando:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

y a continuación

python3.11 get-pip.py --user

Me ha parecido bastante interesante lo de los entornos venv, le echare un vistazo y lo probare.

Mil gracias por todo

1 Like

¡Genial Roberto! Encantado de haberte podido ayudar, de veras que para mi es todo un placer :smiling_face:

Nos vamos viendo por el foro… ¡Abrazos!

1 Like

Hi @frit0-rb , welcome to the Ansible Community Forum!

In addition to the comments by @jbericat, I would suggest you check out the Ansible Execution Environments (also known as ee) and ansible-builder tools.

These features should make it easier to handle different python versions and dependencies while trying to port your playbooks, as they are containers and should allow for better portability between servers as well as for maintaining those environments.

I would advise you to check the following resources:

  1. Getting started with Execution Environments — Ansible Documentation
  2. Unlocking efficiency: Harnessing the capabilities of ansible-builder 3.0
  3. Hands-on Lab: Get started with ansible-navigator
  4. Hands-on Lab: Get started with ansible-builder

The blog post and labs (points 2, 3 and 4) are centered around the Red Hat Ansible Automation Platform product, and might include some features or content not available upstream (like certified collections), you can simply swap those for community components.

The Español (Spanish) category is an all-in-one space for everything in Spanish. All types of discussions, including Help and Development are welcome there.

We are open to people posting in their languages in any other category as well, but thought it would make it easier to have a dedicated space. We also have the more general International Communities category for all languages. Maybe we need to make that more clear somewhere. Thanks for bringing it up!

1 Like

Absolutely! You’re totally right about ee’s :+1: In fact, that’s the way to go for me when using Ansible from CLI - Of course along with ansible-navigator, which on my mind is one of the greatest devtools of the whole Ansible’s ecosystem :wink:

Thank you very much for providing clarification, Leo. Actually, your explanation truly makes sense, since it would indeed be quite cumbersome to replicate all the “main” categories for every International Communities. I’ll definitely remember this moving forward!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.