# Change python to use ansible

**URL:** <https://forum.ansible.com/t/change-python-to-use-ansible/2899>\
**Category:** Get Help\
**Tags:** venv, rockylinux, python, ee, ansible-core\
**Created:** [December 14, 2023, 8:06am UTC](https://forum.ansible.com/t/change-python-to-use-ansible/2899 "2023-12-14T08:06:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![frit0-rb](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/frit0-rb/32/1790_2.png) [@frit0-rb](https://forum.ansible.com/u/frit0-rb)\
**Post date:** [December 14, 2023, 8:06am UTC](https://forum.ansible.com/t/change-python-to-use-ansible/2899/1 "2023-12-14T08:06:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [December 14, 2023, 10:13am UTC](https://forum.ansible.com/t/change-python-to-use-ansible/2899/2 "2023-12-14T10:13:19Z")

</div>

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](https://python.land/virtual-environments/virtualenv). 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:

```bash
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:

```bash
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:

```bash
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:

```bash
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** :

```bash
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 🙂

Hope it helps!

---

<div class="post-metadata">

**Author:** ![frit0-rb](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/frit0-rb/32/1790_2.png) [@frit0-rb](https://forum.ansible.com/u/frit0-rb)\
**Post date:** [December 14, 2023, 10:21am UTC](https://forum.ansible.com/t/change-python-to-use-ansible/2899/3 "2023-12-14T10:21:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [December 14, 2023, 11:19am UTC](https://forum.ansible.com/t/change-python-to-use-ansible/2899/4 "2023-12-14T11:19:55Z")

</div>

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

> [@frit0-rb](#):
>
> First of all, If I create a new venv, what thats means?

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:

```auto
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:

```auto
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 😉

> **[Python venv: How To Create, Activate, Deactivate, And Delete • Python Land...](https://python.land/virtual-environments/virtualenv)**
>
> How to create, activate, use, and delete a Python venv on Windows, Linux, and MacOS. We'll also look at how a Python venv works internally.

> [@frit0-rb](#):
>
> Second, If I install a new ansible-core, all inventory for the actual is it necessary to recreate or how?

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)

> [@frit0-rb](#):
>
> 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

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:

```auto
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](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 😉 What do you think @gwmngilfen @Leo ?

---

<div class="post-metadata">

**Author:** ![frit0-rb](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/frit0-rb/32/1790_2.png) [@frit0-rb](https://forum.ansible.com/u/frit0-rb)\
**Post date:** [December 14, 2023, 11:28am UTC](https://forum.ansible.com/t/change-python-to-use-ansible/2899/5 "2023-12-14T11:28:12Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [December 14, 2023, 11:30am UTC](https://forum.ansible.com/t/change-python-to-use-ansible/2899/6 "2023-12-14T11:30:40Z")

</div>

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

Nos vamos viendo por el foro… ¡Abrazos!

---

<div class="post-metadata">

**Author:** ![Leo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/leo/32/23_2.png) [@Leo](https://forum.ansible.com/u/Leo)\
**Post date:** [December 21, 2023, 6:41am UTC](https://forum.ansible.com/t/change-python-to-use-ansible/2899/7 "2023-12-21T06:41:49Z")

</div>

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](https://docs.ansible.com/ansible/devel/getting_started_ee/index.html)
2. [Unlocking efficiency: Harnessing the capabilities of ansible-builder 3.0](https://www.ansible.com/blog/unlocking-efficiency-harnessing-the-capabilities-of-ansible-builder-3.0)
3. [Hands-on Lab: Get started with ansible-navigator](https://play.instruqt.com/embed/redhat/tracks/getting-started-ansible-navigator?token=em_gvby3leayr2ogdot&show_challenges=true)
4. [Hands-on Lab: Get started with ansible-builder](https://play.instruqt.com/embed/redhat/tracks/getting-started-ansible-builder?token=em_1Ih9nv5b7mX5AfJF&show_challenges=true)

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.

> [@jbericat](#):
>
> 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 😉 What do you think @gwmngilfen @Leo ?

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!

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [December 26, 2023, 7:47pm UTC](https://forum.ansible.com/t/change-python-to-use-ansible/2899/8 "2023-12-26T19:47:57Z")

</div>

> [@Leo](#):
>
> I would suggest you check out the Ansible Execution Environments (also known as #ee) and #ansible-builder tools.

Absolutely! You’re totally right about #ee’s 👍 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 😉

> [@Leo](#):
>
> 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.

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 25, 2024, 7:48pm UTC](https://forum.ansible.com/t/change-python-to-use-ansible/2899/9 "2024-01-25T19:48:28Z")

</div>

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