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 nmcli.py 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
      ip4: 192.0.2.100/24
      gw4: 192.0.2.1
      ip6: '2001:db8::cafe'
      gw6: '2001:db8::1'
      state: present

from https://docs.ansible.com/ansible/2.5/modules/nmcli_module.html

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

Traceback (most recent call last):
File "test.py", line 6, in
gi.require_version('NMClient', '1.2')
File "/usr/lib/python3/dist-packages/gi/init.py", 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/nmcli.py ):

try:
    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:

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

    from gi.repository import NM

And playbook work’s as expected

https://github.com/ansible/ansible/pull/65726/commits/dbe0c95a7a919e87695b2c3e910e12cb7ad02371

https://developer.gnome.org/libnm/stable/usage.html

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