Search code examples
pythonansiblesuds

Failed to import the required Python library (suds) on hosts's Python /usr/bin/python3


While executing the playbook below:

  - name: GetProcessList with sysnr
    community.sap_libs.sap_control_exec:
      hostname: all
      sysnr: "00"
      function: GetProcessList

I get the following error:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'suds'
fatal: [hostname]: FAILED! => changed=false
  msg: Failed to import the required Python library (suds) on hostname'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

After executing it with -vvvv option, I get the below information:

The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_community.sap_libs.sap_control_exec_payload_da6n801n/ansible_community.sap_libs.sap_control_exec_payload.zip/ansible_collections/community/sap_libs/plugins/modules/sap_control_exec.py", line 264, in <module>
ModuleNotFoundError: No module named 'suds'
fatal: [hostname]: FAILED! => changed=false
  invocation:
    module_args:
      force: false
      function: GetProcessList
      hostname: hostname
      parameter: null
      password: null
      port: null
      sysnr: '"00"'
      username: null
  msg: Failed to import the required Python library (suds) on hostname'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

I tried to install suds using pip install suds-py3, but it didn't seem to fix the issue. So, I'm a bit puzzled. Do I need to install additional modules?

Any help will be greatly appreciated. Perhaps, I'm missing something obvious.

Thank you!


Solution

  • The problem is, likely, caused by the fact the library is installed on the ansible controller host (where you execute ansible), not on the host you're configuring (specified in the inventory and hosts: of the play).

    I recommend including something like this

    - name: Fallback in case of errors in pip version detection
      block:
      - name: Get pip version
        command: '{{ ansible_python_interpreter | default("python3") }} -m pip --version'
        register: pip_version
        # stdout: pip 24.0 from /home/myuser/.local/lib/python3.11/site-packages/pip (python 3.11)
    
      - name: Extract pip version from the stdout
        set_fact:
          pip_version: '{{ pip_version.stdout | regex_search("pip ([0-9.]+)") | split(" ") | last }}'
      rescue:
      - name: Assume a recent pip version
        set_fact:
          pip_version: 21.3
    
    - name: Suds library is installed
      pip:
        name: suds-py3
        extra_args: '{{ "--break-system-packages" if pip_version is version("21.3", ">=") else "" }}'
    

    before your task to make sure the required library is always installed.

    Moreover, this way it will be definitely installed for the python ansible is currently using (see ansible_python_interpreter)