# Buggy code
def doImageDownload(module, prompt, answer):
    protocol = module.params["protocol"].lower()
    server = module.params["serverip"]
    imgPath = module.params["imgpath"]
    imgType = module.params["imgtype"]
    username = module.params["serverusername"]
    password = module.params["serverpassword"]
    retVal = ""
    command = "copy " + protocol + " " + protocol + "://" + username + "@"
    command = command + server + "/" + imgPath + " system-image "
    command = command + imgType + " vrf management"
    cmd = []
    if protocol == "scp":
        prompt = [
            "timeout",
            "Confirm download operation",
            "Password",
            "Do you want to change that to the standby image",
        ]
        answer = ["240", "y", password, "y"]
        scp_cmd = [
            {"command": command, "prompt": prompt, "answer": answer, "check_all": True}
        ]
        cmd.extend(scp_cmd)
        retVal = retVal + str(cnos.run_cnos_commands(module, cmd))
    elif protocol == "sftp":
        prompt = [
            "Confirm download operation",
            "Password",
            "Do you want to change that to the standby image",
        ]
        answer = ["y", password, "y"]
        sftp_cmd = [
            {"command": command, "prompt": prompt, "answer": answer, "check_all": True}
        ]
        cmd.extend(sftp_cmd)
        retVal = retVal + str(cnos.run_cnos_commands(module, cmd))
    elif protocol == "ftp":
        prompt = [
            "Confirm download operation",
            "Password",
            "Do you want to change that to the standby image",
        ]
        answer = ["y", password, "y"]
        ftp_cmd = [
            {"command": command, "prompt": prompt, "answer": answer, "check_all": True}
        ]
        cmd.extend(ftp_cmd)
        retVal = retVal + str(cnos.run_cnos_commands(module, cmd))
    elif protocol == "tftp":
        command = "copy " + protocol + " " + protocol + "://" + server
        command = command + "/" + imgPath + " system-image " + imgType
        command = command + +" vrf management"
        prompt = [
            "Confirm download operation",
            "Do you want to change that to the standby image",
        ]
        answer = ["y", "y"]
        tftp_cmd = [
            {"command": command, "prompt": prompt, "answer": answer, "check_all": True}
        ]
        cmd.extend(tftp_cmd)
        retVal = retVal + str(cnos.run_cnos_commands(module, cmd))
    else:
        return "Error-110"

    return retVal


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/home/vagrant/.ansible/tmp/ansible-tmp-1548149534.5508606-278949110624512/AnsiballZ_apt_repository.py", line 113, in <module>
_ansiballz_main()
File "/home/vagrant/.ansible/tmp/ansible-tmp-1548149534.5508606-278949110624512/AnsiballZ_apt_repository.py", line 105, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/home/vagrant/.ansible/tmp/ansible-tmp-1548149534.5508606-278949110624512/AnsiballZ_apt_repository.py", line 48, in invoke_module
imp.load_module('__main__', mod, module, MOD_DESC)
File "/usr/lib/python3.7/imp.py", line 234, in load_module
return load_source(name, filename, file)
File "/usr/lib/python3.7/imp.py", line 169, in load_source
module = _exec(spec, sys.modules[name])
File "<frozen importlib._bootstrap>", line 630, in _exec
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/tmp/ansible_apt_repository_payload_fib2nr65/__main__.py", line 550, in <module>
File "/tmp/ansible_apt_repository_payload_fib2nr65/__main__.py", line 518, in main
File "/tmp/ansible_apt_repository_payload_fib2nr65/__main__.py", line 436, in add_source
File "/tmp/ansible_apt_repository_payload_fib2nr65/__main__.py", line 349, in _add_valid_source
RuntimeError: generator raised StopIteration

# Fixed code
def doImageDownload(module, prompt, answer):
    protocol = module.params["protocol"].lower()
    server = module.params["serverip"]
    imgPath = module.params["imgpath"]
    imgType = module.params["imgtype"]
    username = module.params["serverusername"]
    password = module.params["serverpassword"]
    retVal = ""
    command = "copy " + protocol + " " + protocol + "://" + username + "@"
    command = command + server + "/" + imgPath + " system-image "
    command = command + imgType + " vrf management"
    cmd = []
    if protocol == "scp":
        prompt = [
            "timeout",
            "Confirm download operation",
            "Password",
            "Do you want to change that to the standby image",
        ]
        answer = ["240", "y", password, "y"]
        scp_cmd = [
            {"command": command, "prompt": prompt, "answer": answer, "check_all": True}
        ]
        cmd.extend(scp_cmd)
        retVal = retVal + str(cnos.run_cnos_commands(module, cmd))
    elif protocol == "sftp":
        prompt = [
            "Confirm download operation",
            "Password",
            "Do you want to change that to the standby image",
        ]
        answer = ["y", password, "y"]
        sftp_cmd = [
            {"command": command, "prompt": prompt, "answer": answer, "check_all": True}
        ]
        cmd.extend(sftp_cmd)
        retVal = retVal + str(cnos.run_cnos_commands(module, cmd))
    elif protocol == "ftp":
        prompt = [
            "Confirm download operation",
            "Password",
            "Do you want to change that to the standby image",
        ]
        answer = ["y", password, "y"]
        ftp_cmd = [
            {"command": command, "prompt": prompt, "answer": answer, "check_all": True}
        ]
        cmd.extend(ftp_cmd)
        retVal = retVal + str(cnos.run_cnos_commands(module, cmd))
    elif protocol == "tftp":
        command = "copy " + protocol + " " + protocol + "://" + server
        command = command + "/" + imgPath + " system-image " + imgType
        command = command + " vrf management"
        prompt = [
            "Confirm download operation",
            "Do you want to change that to the standby image",
        ]
        answer = ["y", "y"]
        tftp_cmd = [
            {"command": command, "prompt": prompt, "answer": answer, "check_all": True}
        ]
        cmd.extend(tftp_cmd)
        retVal = retVal + str(cnos.run_cnos_commands(module, cmd))
    else:
        return "Error-110"

    return retVal