Ansible. nmcli module. Failed to import the required Python library

Автор | 09.06.2020

In new version of Ubuntu/Centos need to use new name of imported module. When using old i had this error:

Failed to import the required Python library (NetworkManager glib API) on node's Python /usr/bin/python3. Please read module documentation and install 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"}

when running playbook:

  - nmcli:
      conn_name: my-eth1
      ifname: eth1
      type: ethernet
      ip6: '2001:db8::cafe'
      gw6: '2001:db8::1'
      state: present


or this error when try to use NMClient from python script:

Traceback (most recent call last):
File "", line 6, in
gi.require_version('NMClient', '1.2')
File "/usr/lib/python3/dist-packages/gi/", line 129, in require_version
raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace NMClient not available

If we look in source code of module nmcli in line 567 ( /usr/local/lib/python3.6/dist-packages/ansible/modules/net_tools/ ):

    import gi
    gi.require_version('NMClient', '1.0')
    gi.require_version('NetworkManager', '1.0')

    from gi.repository import NetworkManager, NMClient

In new version we need to import new name NM , like this:

    import gi
    gi.require_version('NM', '1.0')

    from gi.repository import NM

And playbook work’s as expected

Залишити відповідь