Search code examples
pythonamazon-web-servicesansibleamazon-linux-2

Ansible and Amazon Linux 2: How can I use yum module with Python3?


I use Ansible 2.9 to create EC2 instances with Amazon Linux 2. For some purposes I need Python3 on EC2.

So I use option ansible_python_interpreter: "/usr/bin/python3"

But with this option module yum return error pkg_mgr: yum msg: The Python 2 bindings for rpm are needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.

But Amazon Lunux 2 doesn't work with dnf.

The same issue is described here Ansible error: "The Python 2 bindings for rpm are needed for this module", and in other forums. Everywhere suggested solution is Python2.

Is there any way to use Python3 and yum? Or the only way is to use shell module instead?


Solution

  • According information gathered from other site, you can separate yum and non-yum tasks and use python3 only with non yum tasks:

    - hosts: testsv
      gather_facts: no
      become: yes
     
      tasks:
        # here yum running under python2 without errors
        - name: task1 
          yum:
            list: curl  
    
        # here yum running under python3
        - name: task2 
          yum:
            list: curl
          vars:
            ansible_python_interpreter: /usr/bin/python3
    

    Or try to invert condition:

    - hosts: testsv
      gather_facts: no
      become: yes
     
      tasks:
        # Run yum under python2, and all other tasks under python3
        - name: task2 
          yum:
            list: curl
          vars:
            ansible_python_interpreter: /usr/bin/python2