How can I make Ansible use newer python due to errors when running playbook under other version?

We are in a vagrant environment RHEL8.9
The core python is still 3.6.8…

During installation of some core packages with yum/dnf like emacs we get python crypto errors saying that 3.6.8 is no longer supported.

In the vagrantfile

# Install Ansible
dnf -y install python3 &>/dev/null
pip3 install --upgrade pip &>/dev/null
pip3 install ansible &>/dev/null
echo "cd /vagrant" >> /home/vagrant/.bashrc
SCRIPT

ansible --version yields

[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. Current version: 3.6.8 
(default, Jan  5 2024, 08:58:17) [GCC 8.5.0 20210514 (Red Hat 8.5.0-20)]. This feature will be removed from ansible-core in version 2.12. 
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
/usr/local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6.
  from cryptography.exceptions import InvalidSignature
ansible [core 2.11.12] 
  config file = /vagrant/ansible.cfg
  configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  ansible collection location = /home/vagrant/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.6.8 (default, Jan  5 2024, 08:58:17) [GCC 8.5.0 20210514 (Red Hat 8.5.0-20)]
  jinja version = 3.0.3
  libyaml = True

The original run of vagrant ends up installing 3.11 python on the vagrant but the vagrant script ansible under 3.6.8 and not 3.11.

I’m not sure how to set up the script so that I can who what version of python installed by the

dnf -y install python3 &>/dev/null

so that I can install virtualenv and then create a virtualenv based on that newer python and then install ansieble in that newer python version…hope this is somewhat clear.

RHEL 8.9 has python3.6, 3.8, 3.9, and 3.11 available to it. It uses alternatives to set the priority on them, and pip3 inherits whatever python3 is set to. Since you’re not enforcing a version, it’s apparently defaulting to python3.6 and pip3 is installing based on that.

Possible solutions:

  1. Be more specific in your installation versions. dnf install -y python3.11-pip && pip3.11 install -U ansible
  2. Update your alternatives: dnf install -y python3.11-pip && update-alternatives --set python3 /usr/bin/python3.11 && pip3 install -U ansible
  3. Install EPEL and use the ansible-core version provided by AppStream dnf install -y ansible # this is what I do, but EPEL is tailored and provided through Satellite Content Views instead of installing epel-release.
2 Likes

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