From f8b7ab1e10e582f09ef453f437fb2d4bc1ecc2f0 Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Tue, 6 Sep 2016 15:11:36 +0200 Subject: [PATCH] Do not crash if the system do not have required modules (#2852) --- lib/ansible/modules/extras/network/nmcli.py | 22 ++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/extras/network/nmcli.py b/lib/ansible/modules/extras/network/nmcli.py index a7746051099..64ee02b09c5 100644 --- a/lib/ansible/modules/extras/network/nmcli.py +++ b/lib/ansible/modules/extras/network/nmcli.py @@ -381,9 +381,19 @@ tenant_ip: "192.168.200.21/23" # import ansible.module_utils.basic import os import sys -import dbus -from gi.repository import NetworkManager, NMClient - +HAVE_DBUS=False +try: + import dbus + HAVE_DBUS=True +except ImportError: + pass + +HAVE_NM_CLIENT=False +try: + from gi.repository import NetworkManager, NMClient + HAVE_NM_CLIENT=True +except ImportError: + pass class Nmcli(object): """ @@ -1010,6 +1020,12 @@ def main(): supports_check_mode=True ) + if not HAVE_DBUS: + module.fail_json(msg="This module requires dbus python bindings") + + if not HAVE_NM_CLIENT: + module.fail_json(msg="This module requires NetworkManager glib API") + nmcli=Nmcli(module) rc=None