# Buggy code
def __init__(self, play_context, new_stdin, *args, **kwargs):
    super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs)

    self._ssh_shell = None

    self._matched_prompt = None
    self._matched_cmd_prompt = None
    self._matched_pattern = None
    self._last_response = None
    self._history = list()

    self._terminal = None
    self.cliconf = None
    self.paramiko_conn = None

    if self._play_context.verbosity > 3:
        logging.getLogger("paramiko").setLevel(logging.DEBUG)

    if self._network_os:
        self.cliconf = cliconf_loader.get(self._network_os, self)
        if self.cliconf:
            display.vvvv("loaded cliconf plugin for network_os %s" % self._network_os)
            self._sub_plugins.append(
                {"type": "cliconf", "name": self._network_os, "obj": self.cliconf}
            )
        else:
            display.vvvv("unable to load cliconf for network_os %s" % self._network_os)
    else:
        raise AnsibleConnectionFailure(
            "Unable to automatically determine host network os. Please "
            "manually configure ansible_network_os value for this host"
        )
    display.display("network_os is set to %s" % self._network_os, log_only=True)


# Traceback (KeyError)
TASK [ios_facts] ***********************************************************************************************************************************************************************************************************************
task path: /home/pffs/change-scripts/ansible/script.yaml:9
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_3_5Dr3/ansible_module_ios_facts.py", line 525, in <module>
main()
File "/tmp/ansible_3_5Dr3/ansible_module_ios_facts.py", line 511, in main
inst.populate()
File "/tmp/ansible_3_5Dr3/ansible_module_ios_facts.py", line 281, in populate
self.populate_ipv4_interfaces(data)
File "/tmp/ansible_3_5Dr3/ansible_module_ios_facts.py", line 316, in populate_ipv4_interfaces
self.facts['interfaces'][key]['ipv4'] = list()
KeyError: u'Serial0/3/0'

fatal: [1.2.3.4]: FAILED! => {
"changed": false,
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def __init__(self, play_context, new_stdin, *args, **kwargs):
    super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs)

    self._ssh_shell = None

    self._matched_prompt = None
    self._matched_cmd_prompt = None
    self._matched_pattern = None
    self._last_response = None
    self._history = list()
    self._command_response = None

    self._terminal = None
    self.cliconf = None
    self.paramiko_conn = None

    if self._play_context.verbosity > 3:
        logging.getLogger("paramiko").setLevel(logging.DEBUG)

    if self._network_os:
        self.cliconf = cliconf_loader.get(self._network_os, self)
        if self.cliconf:
            display.vvvv("loaded cliconf plugin for network_os %s" % self._network_os)
            self._sub_plugins.append(
                {"type": "cliconf", "name": self._network_os, "obj": self.cliconf}
            )
        else:
            display.vvvv("unable to load cliconf for network_os %s" % self._network_os)
    else:
        raise AnsibleConnectionFailure(
            "Unable to automatically determine host network os. Please "
            "manually configure ansible_network_os value for this host"
        )
    display.display("network_os is set to %s" % self._network_os, log_only=True)