skip to Main Content

I’d like to run Ansible tasks with python3 as the interpreter (there are lots of reasons for why to go to python3 … one of them being that python2 will not be supported anymore by Ansible).

Unfortunately, doing that on RedHat 7 is not possible as I can’t install python3-dnf there (it seems this package is available only for RedHat 8).

Does anyone had that issue and founded a solution for it?

Thanks

3

Answers


  1. I’m one if the Ansible maintainers for the yum and dnf modules. Python 2 will continue to be supported for the remote hosts (hosts in the inventory that you are automating tasks on) but not the controller node (where your are actually running the ansible-playbook command). You can safely continue to use Python 2 on the remote RHEL7 hosts so long as you are running Ansible from a control node that’s on Python 3, and even that restriction doesn’t exist for the current stable release of Ansible (2.9.10 at the time of this writing) but only for future releases. Hope this helps, happy automating!

    Login or Signup to reply.
  2. Unfortunally the answer from Adam isnt helpful when you are building roles that should be available for RedHat/Debian/CentOS/… and you use custom libraries that are only available in Python3 (because we have 2021 and Python2 EoL was 2020). So because of the missing Python3 support for yum in CentOS7 you run into a dependency hell problem.

    All hosts (whatever OS) should use Python3 as ansible_python_interpreter.

    All tasks should be running with Python3 (either on controller or on the host). There is one single problem (from my point today) – the task package. Even when it is OS-independent, it requires Python2 on CentOS7 (on all other OS’s there is no problem with Python3).

    What I did – only for this module – switch to Python2 when your host is on CentOS7. For example:

    - name: "Define Python Interpreter"
      set_fact:
        my_interpreter: "{{ ansible_python_interpreter }}"
    
    - name: "Override Python Interpeter for CentOS7"
      set_fact:
        my_interpreter: "/usr/bin/python"
      when:
        - "ansible_distribution == 'CentOS'"
        - "ansible_distribution_major_version | int == 7"
    
    - name: "Install dependencies"
      package:
        name: "...."
      become: true
      vars:
        ansible_python_interpreter: "{{ my_interpreter }}"
    

    In all other cases stay with your configured host specific Python interpreter.

    Login or Signup to reply.
  3. If you want to use python3 on CentOS 7, then instead of using "dnf" module, you can shell command directly, for example:

        - name: Uninstall foo and bar (CentOS)
          become: yes
          shell:
            cmd: "if yum list installed {{ item }};
                  then
                    yum -y remove {{ item }};
                  fi"
            warn: false  # Use shell to workaround the problem with python dnf module: https://github.com/ansible/ansible/issues/71668
          loop:
            - foo
            - bar
    

    My test shows that the only case that ansible works with python3 is that they are on the same machine and when ansible is installed with

    python3 -m pip install ansible
    

    which is a special situation where the ansible you run on the target machine (tested with yum module but presume it will work with dnf module).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search