How to configure the newly installed Python 3.8 on RockyLinux 8.10 while I want to running ansible task on this VM as a target node?

As title,
I have a EC2 VM with RockyLinux and I execute this command to install an newly Python 3.8
```shell
sudo dnf module -y install python38
```
And python3 –version, show me that
Python 3.8.17

And, hence, I execute a ansible task on this machine

```yaml

# Disable Selinux

- name: Disable selinux

selinux:

state: disabled
```

, but it show me this error:

The full traceback is:
Traceback (most recent call last):
File “/tmp/ansible_selinux_payload_2yo34_4t/ansible_selinux_payload.zip/ansible_collections/ansible/posix/plugins/modules/selinux.py”, line 100, in
ModuleNotFoundError: No module named ‘selinux’
fatal: [54.250.31.45]: FAILED! => {
“changed”: false,
“invocation”: {
“module_args”: {
“configfile”: “/etc/selinux/config”,
“policy”: null,
“state”: “disabled”,
“update_kernel_param”: false
}
},
“msg”: “Failed to import the required Python library (libselinux-python) on test-prod-common-ec2-logictw’s Python /usr/bin/python3. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter”
}

And if I rollback to previous python3 version(Python 3.6.8) which is installed via command:
```shell
sudo dnf install python3 -y

```

the alternatives cli to rollback to python 3.6
```shell
alternatives --config python3

There are 2 programs which provide ‘python3’.

Selection Command

*+ 1 /usr/bin/python3.6
2 /usr/bin/python3.8

Enter to keep the current selection[+], or type selection number:
```

the ansible task passed.

First, el8 has “platform python” and “user pythons”. The logic is that system tools, like dnf, use the platform python, which is always installed. The user pythons are optional, only for user’s Python applications. Ironically, the ansible-core is a Python application and its el8 version requires Python 3.12. That is, Ansible does not use platform python on control host.

The python3 user python in el8 is version 3.6 merely creates symlinks to platform python.
The other user python versions have their own binaries.


You can install multiple user pythons simultaneously and call each of them explicitly (e.g. python3.6, python3.8, etc). Yes, their installation does modify the symlink /etc/alternatives/python3 and the python3 is symlink to /etc/alternatives/python3. Therefore, if you want to call specific python, you would use, for example /usr/bin/python3.8, not /usr/bin/python3.


You can tell to Ansible via the ansible_python_interpretervar to use on managed host other python than what it finds/defaults to. For example:

ansible_python_interpreter: /usr/libexec/platform-python

Furthermore, the python version in managed host does not have to be the same as in the control host. (You could manage many distros with many pythons from one control host.) Ansible has some minimum requirements, which prevents managing ancient distros.

Overall, the platform python ought to have all necessary modules, while most of the user pythons have rather minimal set of modules. Something like SELinux … you should prefer the platform python as its (system) tools you would use if you would do things manually.

Updating python is not the entire problem. For example, I have EL8 hosts I can no longer adminster because I’m using the Ansible packages on Fedora 42 to run my playbooks. The newer version of Ansible on Fedora 42 causes problems with old machines like RHEL8 or Rocky 8 because the newer version of Ansible has effectively dropped support for those older machines due to the older python versions, etc.

For example I can get something like this when attempting to apply updates to a system, so using the ansible.builtin.package module:

fatal: [rocky8]: FAILED! => {"changed": false, "msg": "Could not import the dnf python module using /usr/bin/python3.12 (3.12.11 (main, Aug 26 2025, 15:44:11) [GCC 8.5.0 20210514 (Red Hat 8.5.0-26)]). Please install `python3-dnf` package or ensure you have specified the correct ansible_python_interpreter. (attempted ['/usr/libexec/platform-python', '/usr/bin/python3', '/usr/bin/python'])", "results": []}

irrespective of how I configure the interpreter, it won’t work as it’s attempted to utilise various python versions as well as the platform-python automatically. And python3-dnf is installed, so it’s not that either. It’s simply due to the fact that the ansible itself is the issue. When I was using an older version of ansible it was not a problem.

Whilst you can specifiy the interpreter it probably isn’t going to help much. In reality, it means using an older version of Ansible to manage/maintain those older RHEL8/Rocky 8 servers.

OK, thanks a lot,
And from ansible docs here,
I should use some older ansible-core to support older python3 version~

Trivia: el9 has ansible-core based on 2.14 and el8 has it based on 2.16.
RH blog on that: Updates to using Ansible Core in Red Hat Enterprise Linux

Both support Python 2.7 (and many python3 versions) on target nodes.