pythonpipansible

Ansible error: "The Python 2 bindings for rpm are needed for this module"


Im trying to pip install a requirements file in my python3 environment using the following task

pip:
  python3: yes
  requirements: ./requirements/my_requirements.txt
  extra_args: -i http://mypypi/windows/simple

I checked which version ansible is running on the controller node (RH7) and it's 3.6.8

ansible-playbook 2.9.9
  config file = None
  configured module search path = ['/home/{hidden}/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.6.8 (default, Jun 11 2019, 15:15:01) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
No config file found; using defaults

I am however getting the following error:

fatal: [default]: FAILED! => {"changed": false, "msg": "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."}

My controller node is running RH7. The targets are centos7 (provisioned by vagrantfiles)

Does anyonek now how to solve this?


Solution

  • I had a similar problem with the "Amazon Linux 2" distribution that uses yum, but does not support dnf as of this writing.

    As mentioned in the comments above, my problem was in the ansible-managed nodes (AWS EC2 instances running Amazon Linux 2) and not in the controller.

    Solved it by imposing the use of python2, adding ansible_python_interpreter=/usr/bin/python2 for this group of hosts in the ansible inventory file, as in the following snippet:

    [amz_linux]
    server2 ansible_host=ec2-xx-yy-zz-pp.eu-west-1.compute.amazonaws.com
    
    
    [amz_linux:vars]
    ansible_user=ec2-user
    ansible_python_interpreter=/usr/bin/python2
    

    Tried it with this playbook, adapted from a Redhat quick guide.

    ---
    - hosts: amz_linux
      become: yes
      tasks:
       - name: install Apache server
         yum:
           name: httpd
           state: latest
    
       - name: enable and start Apache server
         service:
           name: httpd
           enabled: yes
           state: started
    
       - name: create web admin group
         group:
           name: web
           state: present
    
       - name: create web admin user
         user:
           name: webadm
           comment: "Web Admin"
           groups: web
           append: yes
    
       - name: set content directory group/permissions 
         file:
           path: /var/www/html
           owner: root
           group: web
           state: directory
           mode: u=rwx,g=rwx,o=rx,g+s
    
       - name: create default page content
         copy:
           content: "Welcome to {{ ansible_fqdn}} on {{ ansible_default_ipv4.address }}"
           dest: /var/www/html/index.html
           owner: webadm
           group: web
           mode: u=rw,g=rw,o=r
    

    Actual ansible-playbook run (after using ssh-add to add the instance private key to the ssh agent.)

    $ ansible-playbook -i ansible/hosts ansible/apache_amz_linux.yaml 
    
    PLAY [amz_linux] **********************************************************************************************************
    
    TASK [Gathering Facts] ****************************************************************************************************
    The authenticity of host 'ec2-xxxxxxxxxxx.eu-west-1.compute.amazonaws.com (xxxxxxxxxxx)' can't be established.
    ECDSA key fingerprint is SHA256:klksjdflskdjflskdfjsldkfjslkdjflskdjf/sdkfj.
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    ok: [server2]
    
    TASK [install Apache server] **********************************************************************************************
    changed: [server2]
    
    TASK [enable and start Apache server] *************************************************************************************
    changed: [server2]
    
    TASK [create web admin group] *********************************************************************************************
    changed: [server2]
    
    TASK [create web admin user] **********************************************************************************************
    changed: [server2]
    
    TASK [set content directory group/permissions] ****************************************************************************
    changed: [server2]
    
    TASK [create default page content] ****************************************************************************************
    changed: [server2]
    
    PLAY RECAP ****************************************************************************************************************
    server2                    : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0