# Buggy code
def get_cpu_facts(self, collected_facts=None):
    cpu_facts = {}
    collected_facts = collected_facts or {}

    if collected_facts.get("ansible_architecture") == "9000/800":
        rc, out, err = self.module.run_command(
            "ioscan -FkCprocessor | wc -l", use_unsafe_shell=True
        )
        cpu_facts["processor_count"] = int(out.strip())
    # Working with machinfo mess
    elif collected_facts.get("ansible_architecture") == "ia64":
        if collected_facts.get("ansible_distribution_version") == "B.11.23":
            rc, out, err = self.module.run_command(
                "/usr/contrib/bin/machinfo | grep 'Number of CPUs'",
                use_unsafe_shell=True,
            )
            cpu_facts["processor_count"] = int(out.strip().split("=")[1])
            rc, out, err = self.module.run_command(
                "/usr/contrib/bin/machinfo | grep 'processor family'",
                use_unsafe_shell=True,
            )
            cpu_facts["processor"] = re.search(".*(Intel.*)", out).groups()[0].strip()
            rc, out, err = self.module.run_command(
                "ioscan -FkCprocessor | wc -l", use_unsafe_shell=True
            )
            cpu_facts["processor_cores"] = int(out.strip())
        if collected_facts.get("ansible_distribution_version") == "B.11.31":
            # if machinfo return cores strings release B.11.31 > 1204
            rc, out, err = self.module.run_command(
                "/usr/contrib/bin/machinfo | grep core | wc -l", use_unsafe_shell=True
            )
            if out.strip() == "0":
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | grep Intel", use_unsafe_shell=True
                )
                cpu_facts["processor_count"] = int(out.strip().split(" ")[0])
                # If hyperthreading is active divide cores by 2
                rc, out, err = self.module.run_command(
                    "/usr/sbin/psrset | grep LCPU", use_unsafe_shell=True
                )
                data = re.sub(" +", " ", out).strip().split(" ")
                if len(data) == 1:
                    hyperthreading = "OFF"
                else:
                    hyperthreading = data[1]
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | grep logical", use_unsafe_shell=True
                )
                data = out.strip().split(" ")
                if hyperthreading == "ON":
                    cpu_facts["processor_cores"] = int(data[0]) / 2
                else:
                    if len(data) == 1:
                        cpu_facts["processor_cores"] = cpu_facts["processor_count"]
                    else:
                        cpu_facts["processor_cores"] = int(data[0])
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | grep Intel |cut -d' ' -f4-",
                    use_unsafe_shell=True,
                )
                cpu_facts["processor"] = out.strip()
            else:
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | egrep 'socket[s]?$' | tail -1",
                    use_unsafe_shell=True,
                )
                cpu_facts["processor_count"] = int(out.strip().split(" ")[0])
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | grep -e '[0-9] core' | tail -1",
                    use_unsafe_shell=True,
                )
                cpu_facts["processor_cores"] = int(out.strip().split(" ")[0])
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | grep Intel", use_unsafe_shell=True
                )
                cpu_facts["processor"] = out.strip()

    return cpu_facts


# Traceback (nIndexError)
From 2.2:

| FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Warning: Permanently added 't' (RSA) to the list of known hosts.\r\nTraceback (most recent call last):\n  File \"/tmp/ansible_vA4QLB/ansible_module_setup.py\", line 134, in <module>\n    main()\n  File \"/tmp/ansible_vA4QLB/ansible_module_setup.py\", line 126, in main\n    data = get_all_facts(module)\n  File \"/tmp/ansible_vA4QLB/ansible_modlib.zip/ansible/module_utils/facts.py\", line 3518, in get_all_facts\n  File \"/tmp/ansible_vA4QLB/ansible_modlib.zip/ansible/module_utils/facts.py\", line 3461, in ansible_facts\n  File \"/tmp/ansible_vA4QLB/ansible_modlib.zip/ansible/module_utils/facts.py\", line 2002, in populate\n  File \"/tmp/ansible_vA4QLB/ansible_modlib.zip/ansible/module_utils/facts.py\", line 2015, in get_cpu_facts\nIndexError: list index out of range\n",
"module_stdout": "",
"msg": "MODULE FAILURE"
}

# Fixed code
def get_cpu_facts(self, collected_facts=None):
    cpu_facts = {}
    collected_facts = collected_facts or {}

    if collected_facts.get("ansible_architecture") == "9000/800":
        rc, out, err = self.module.run_command(
            "ioscan -FkCprocessor | wc -l", use_unsafe_shell=True
        )
        cpu_facts["processor_count"] = int(out.strip())
    # Working with machinfo mess
    elif collected_facts.get("ansible_architecture") == "ia64":
        if collected_facts.get("ansible_distribution_version") == "B.11.23":
            rc, out, err = self.module.run_command(
                "/usr/contrib/bin/machinfo | grep 'Number of CPUs'",
                use_unsafe_shell=True,
            )
            if out:
                cpu_facts["processor_count"] = int(out.strip().split("=")[1])
            rc, out, err = self.module.run_command(
                "/usr/contrib/bin/machinfo | grep 'processor family'",
                use_unsafe_shell=True,
            )
            if out:
                cpu_facts["processor"] = (
                    re.search(".*(Intel.*)", out).groups()[0].strip()
                )
            rc, out, err = self.module.run_command(
                "ioscan -FkCprocessor | wc -l", use_unsafe_shell=True
            )
            cpu_facts["processor_cores"] = int(out.strip())
        if collected_facts.get("ansible_distribution_version") == "B.11.31":
            # if machinfo return cores strings release B.11.31 > 1204
            rc, out, err = self.module.run_command(
                "/usr/contrib/bin/machinfo | grep core | wc -l", use_unsafe_shell=True
            )
            if out.strip() == "0":
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | grep Intel", use_unsafe_shell=True
                )
                cpu_facts["processor_count"] = int(out.strip().split(" ")[0])
                # If hyperthreading is active divide cores by 2
                rc, out, err = self.module.run_command(
                    "/usr/sbin/psrset | grep LCPU", use_unsafe_shell=True
                )
                data = re.sub(" +", " ", out).strip().split(" ")
                if len(data) == 1:
                    hyperthreading = "OFF"
                else:
                    hyperthreading = data[1]
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | grep logical", use_unsafe_shell=True
                )
                data = out.strip().split(" ")
                if hyperthreading == "ON":
                    cpu_facts["processor_cores"] = int(data[0]) / 2
                else:
                    if len(data) == 1:
                        cpu_facts["processor_cores"] = cpu_facts["processor_count"]
                    else:
                        cpu_facts["processor_cores"] = int(data[0])
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | grep Intel |cut -d' ' -f4-",
                    use_unsafe_shell=True,
                )
                cpu_facts["processor"] = out.strip()
            else:
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | egrep 'socket[s]?$' | tail -1",
                    use_unsafe_shell=True,
                )
                cpu_facts["processor_count"] = int(out.strip().split(" ")[0])
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | grep -e '[0-9] core' | tail -1",
                    use_unsafe_shell=True,
                )
                cpu_facts["processor_cores"] = int(out.strip().split(" ")[0])
                rc, out, err = self.module.run_command(
                    "/usr/contrib/bin/machinfo | grep Intel", use_unsafe_shell=True
                )
                cpu_facts["processor"] = out.strip()

    return cpu_facts