Pyhton2 Interpreter on CentOS 7

If you get following error message on a CentOS/RedHat 7 target system:

"The Python 2 bindings for rpm are needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.. The Python 2 yum module is needed for this module. If you require Python 3 support use the `dnf` Ansible module instead."}

The package module on CentOS 7 needs python2 instead of python3, because the python-dnf is only available for python2. You have to change the default python interpreter.

First include vars in your playbook:

---
- hosts: all

  tasks:
    - name: System-spezifische Parameter laden (not RH/CO)
      include_vars: apache_{{ansible_os_family}}.yml
      when: ansible_facts['os_family'] != "RedHat"

    - name: System-spezifische Parameter laden (RH/CO)
      include_vars: apache_{{ansible_os_family}}{{ansible_distribution_major_version}}.yml
      when: ansible_facts['os_family'] == "RedHat"

    - name: Apache-Paket installieren
      package:
        name: "{{apache_package_name}}"

    - name: Dienst starten und in Bootprozess integrieren
      service:
        name: "{{apache_service_name}}"
        state: started
        enabled: yes

In the vars directory you now create for each linux os a import yml file - and two for RedHat:

apache_Debian.yml
apache_RedHat7.yml
apache_RedHat8.yml
apache_Suse.yml
python_co78.yml

Set other python interpreter for CentOS/RedHat 7:

---
apache_package_name: httpd
apache_service_name: httpd
apache_document_root: /var/www/html
apache_config_directory: /etc/httpd/conf.d
ansible_python_interpreter: /usr/bin/python

---
apache_package_name: httpd
apache_service_name: httpd
apache_document_root: /var/www/html
apache_config_directory: /etc/httpd/conf.d



Authors:
  • Jochen Schnuerle