# Buggy code
def list_connection_info(self):
    # Ask the settings service for the list of connections it provides
    bus = dbus.SystemBus()

    service_name = "org.freedesktop.NetworkManager"
    proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager/Settings")
    settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
    connection_paths = settings.ListConnections()
    connection_list = []
    # List each connection's name, UUID, and type
    for path in connection_paths:
        con_proxy = bus.get_object(service_name, path)
        settings_connection = dbus.Interface(
            con_proxy, "org.freedesktop.NetworkManager.Settings.Connection"
        )
        config = settings_connection.GetSettings()

        # Now get secrets too; we grab the secrets for each type of connection
        # (since there isn't a "get all secrets" call because most of the time
        # you only need 'wifi' secrets or '802.1x' secrets, not everything) and
        # merge that into the configuration data - To use at a later stage
        self.merge_secrets(settings_connection, config, "802-11-wireless")
        self.merge_secrets(settings_connection, config, "802-11-wireless-security")
        self.merge_secrets(settings_connection, config, "802-1x")
        self.merge_secrets(settings_connection, config, "gsm")
        self.merge_secrets(settings_connection, config, "cdma")
        self.merge_secrets(settings_connection, config, "ppp")

        # Get the details of the 'connection' setting
        s_con = config["connection"]
        connection_list.append(s_con["id"])
        connection_list.append(s_con["uuid"])
        connection_list.append(s_con["type"])
        connection_list.append(self.connection_to_string(config))
    return connection_list


# Traceback (org.freedesktop.DBus.Error)
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/net_tools/nmcli.py
<mainframe> ESTABLISH SSH CONNECTION FOR USER: ansible
<mainframe> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=10 -o ControlPath=/home/coaxial/.ansible/cp/c6e583ef4b mainframe '/bin/sh -c '"'"'echo ~ &amp;&amp; sleep 0'"'"''
<mainframe> (0, '/home/ansible\n', '')
<mainframe> ESTABLISH SSH CONNECTION FOR USER: ansible
<mainframe> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=10 -o ControlPath=/home/coaxial/.ansible/cp/c6e583ef4b mainframe '/bin/sh -c '"'"'( umask 77 &amp;&amp; mkdir -p "` echo /home/ansible/.ansible/tmp/ansible-tmp-1515287285.04-40999467661592 `" &amp;&amp; echo ansible-tmp-1515287285.04-40999467661592="` echo /home/ansible/.ansible/tmp/ansible-tmp-1515287285.04-40999467661592 `" ) &amp;&amp; sleep 0'"'"''
<mainframe> (0, 'ansible-tmp-1515287285.04-40999467661592=/home/ansible/.ansible/tmp/ansible-tmp-1515287285.04-40999467661592\n', '')
<mainframe> PUT /tmp/tmpHiYrgB TO /home/ansible/.ansible/tmp/ansible-tmp-1515287285.04-40999467661592/nmcli.py
<mainframe> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=10 -o ControlPath=/home/coaxial/.ansible/cp/c6e583ef4b '[mainframe]'
<mainframe> (0, 'sftp> put /tmp/tmpHiYrgB /home/ansible/.ansible/tmp/ansible-tmp-1515287285.04-40999467661592/nmcli.py\n', '')
<mainframe> ESTABLISH SSH CONNECTION FOR USER: ansible
<mainframe> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=10 -o ControlPath=/home/coaxial/.ansible/cp/c6e583ef4b mainframe '/bin/sh -c '"'"'chmod u+x /home/ansible/.ansible/tmp/ansible-tmp-1515287285.04-40999467661592/ /home/ansible/.ansible/tmp/ansible-tmp-1515287285.04-40999467661592/nmcli.py &amp;&amp; sleep 0'"'"''
<mainframe> (0, '', '')
<mainframe> ESTABLISH SSH CONNECTION FOR USER: ansible
<mainframe> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ansible -o ConnectTimeout=10 -o ControlPath=/home/coaxial/.ansible/cp/c6e583ef4b -tt mainframe '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-qwypckfxnekvxuvyopnpjsdhiesotuae; /usr/bin/python /home/ansible/.ansible/tmp/ansible-tmp-1515287285.04-40999467661592/nmcli.py; rm -rf "/home/ansible/.ansible/tmp/ansible-tmp-1515287285.04-40999467661592/" > /dev/null 2>&amp;1'"'"'"'"'"'"'"'"' &amp;&amp; sleep 0'"'"''
<mainframe> (0, '/tmp/ansible_iyby84/ansible_module_nmcli.py:493: PyGIWarning: NetworkManager was imported without specifying a version first. Use gi.require_version(\'NetworkManager\', \'1.0\') before import to ensure that the right version gets loaded.\r\n  from gi.repository import NetworkManager, NMClient\r\n/tmp/ansible_iyby84/ansible_module_nmcli.py:493: PyGIWarning: NMClient was imported without specifying a version first. Use gi.require_version(\'NMClient\', \'1.0\') before import to ensure that the right version gets loaded.\r\n  from gi.repository import NetworkManager, NMClient\r\nTraceback (most recent call last):\r\n  File "/tmp/ansible_iyby84/ansible_module_nmcli.py", line 1190, in <module>\r\n    main()\r\n  File "/tmp/ansible_iyby84/ansible_module_nmcli.py", line 1162, in main\r\n    if nmcli.connection_exists():\r\n  File "/tmp/ansible_iyby84/ansible_module_nmcli.py", line 673, in connection_exists\r\n    connections=self.list_connection_info()\r\n  File "/tmp/ansible_iyby84/ansible_module_nmcli.py", line 642, in list_connection_info\r\n    proxy=bus.get_object(service_name, "/org/freedesktop/NetworkManager/Settings")\r\n  File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 241, in get_object\r\n    follow_name_owner_changes=follow_name_owner_changes)\r\n  File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 248, in __init__\r\n    self._named_service = conn.activate_name_owner(bus_name)\r\n  File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 180, in activate_name_owner\r\n    self.start_service_by_name(bus_name)\r\n  File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 278, in start_service_by_name\r\n    \'su\', (bus_name, flags)))\r\n  File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking\r\n    message, timeout)\r\ndbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.NetworkManager was not provided by any .service files\r\n', 'Shared connection to mainframe closed.\r\n')
fatal: [mainframe]: FAILED! => {
"changed": false,
"module_stderr": "Shared connection to mainframe closed.\r\n",
"module_stdout": "/tmp/ansible_iyby84/ansible_module_nmcli.py:493: PyGIWarning: NetworkManager was imported without specifying a version first. Use gi.require_version('NetworkManager', '1.0') before import to ensure that the right version gets loaded.\r\n  from gi.repository import NetworkManager, NMClient\r\n/tmp/ansible_iyby84/ansible_module_nmcli.py:493: PyGIWarning: NMClient was imported without specifying a version first. Use gi.require_version('NMClient', '1.0') before import to ensure that the right version gets loaded.\r\n  from gi.repository import NetworkManager, NMClient\r\nTraceback (most recent call last):\r\n  File \"/tmp/ansible_iyby84/ansible_module_nmcli.py\", line 1190, in <module>\r\n    main()\r\n  File \"/tmp/ansible_iyby84/ansible_module_nmcli.py\", line 1162, in main\r\n    if nmcli.connection_exists():\r\n  File \"/tmp/ansible_iyby84/ansible_module_nmcli.py\", line 673, in connection_exists\r\n    connections=self.list_connection_info()\r\n  File \"/tmp/ansible_iyby84/ansible_module_nmcli.py\", line 642, in list_connection_info\r\n    proxy=bus.get_object(service_name, \"/org/freedesktop/NetworkManager/Settings\")\r\n  File \"/usr/lib/python2.7/dist-packages/dbus/bus.py\", line 241, in get_object\r\n    follow_name_owner_changes=follow_name_owner_changes)\r\n  File \"/usr/lib/python2.7/dist-packages/dbus/proxies.py\", line 248, in __init__\r\n    self._named_service = conn.activate_name_owner(bus_name)\r\n  File \"/usr/lib/python2.7/dist-packages/dbus/bus.py\", line 180, in activate_name_owner\r\n    self.start_service_by_name(bus_name)\r\n  File \"/usr/lib/python2.7/dist-packages/dbus/bus.py\", line 278, in start_service_by_name\r\n    'su', (bus_name, flags)))\r\n  File \"/usr/lib/python2.7/dist-packages/dbus/connection.py\", line 651, in call_blocking\r\n    message, timeout)\r\ndbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.NetworkManager was not provided by any .service files\r\n",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def list_connection_info(self):
    # Ask the settings service for the list of connections it provides
    bus = dbus.SystemBus()

    service_name = "org.freedesktop.NetworkManager"
    settings = None
    try:
        proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager/Settings")
        settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
    except dbus.Exceptions.DBusException as e:
        self.module.fail_json(
            msg="Unable to read Network Manager settings from DBus system bus: %s"
            % to_native(e),
            details="Please check if NetworkManager is installed and"
            " service network-manager is started.",
        )
    connection_paths = settings.ListConnections()
    connection_list = []
    # List each connection's name, UUID, and type
    for path in connection_paths:
        con_proxy = bus.get_object(service_name, path)
        settings_connection = dbus.Interface(
            con_proxy, "org.freedesktop.NetworkManager.Settings.Connection"
        )
        config = settings_connection.GetSettings()

        # Now get secrets too; we grab the secrets for each type of connection
        # (since there isn't a "get all secrets" call because most of the time
        # you only need 'wifi' secrets or '802.1x' secrets, not everything) and
        # merge that into the configuration data - To use at a later stage
        self.merge_secrets(settings_connection, config, "802-11-wireless")
        self.merge_secrets(settings_connection, config, "802-11-wireless-security")
        self.merge_secrets(settings_connection, config, "802-1x")
        self.merge_secrets(settings_connection, config, "gsm")
        self.merge_secrets(settings_connection, config, "cdma")
        self.merge_secrets(settings_connection, config, "ppp")

        # Get the details of the 'connection' setting
        s_con = config["connection"]
        connection_list.append(s_con["id"])
        connection_list.append(s_con["uuid"])
        connection_list.append(s_con["type"])
        connection_list.append(self.connection_to_string(config))
    return connection_list