# 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,
            )
            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


# Traceback (nAttributeError)
Loading callback plugin minimal of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc
META: ran handlers
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
<xxx.xxx.xxx.xxx> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO xxx.xxx.xxx.xxx
<xxx.xxx.xxx.xxx> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1508496494.76-29371413675763 `" &amp;&amp; echo ansible-tmp-1508496494.76-29371413675763="` echo $HOME/.ansible/tmp/ansible-tmp-1508496494.76-29371413675763 `" ) &amp;&amp; sleep 0'
<xxx.xxx.xxx.xxx> PUT /tmp/tmpXmIMND TO //.ansible/tmp/ansible-tmp-1508496494.76-29371413675763/setup.py
<xxx.xxx.xxx.xxx> EXEC /bin/sh -c 'chmod u+x //.ansible/tmp/ansible-tmp-1508496494.76-29371413675763/ //.ansible/tmp/ansible-tmp-1508496494.76-29371413675763/setup.py &amp;&amp; sleep 0'
<xxx.xxx.xxx.xxx> EXEC /bin/sh -c '/opt/PBpython/bin/python //.ansible/tmp/ansible-tmp-1508496494.76-29371413675763/setup.py; rm -rf "//.ansible/tmp/ansible-tmp-1508496494.76-29371413675763/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
HOSTNAME-REDACTED| FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "",
"module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_81XLGz/ansible_module_setup.py\", line 139, in <module>\r\n    main()\r\n  File \"/tmp/ansible_81XLGz/ansible_module_setup.py\", line 131, in main\r\n    data = get_all_facts(module)\r\n  File \"/tmp/ansible_81XLGz/ansible_modlib.zip/ansible/module_utils/facts.py\", line 3939, in get_all_facts\r\n  File \"/tmp/ansible_81XLGz/ansible_modlib.zip/ansible/module_utils/facts.py\", line 3882, in ansible_facts\r\n  File \"/tmp/ansible_81XLGz/ansible_modlib.zip/ansible/module_utils/facts.py\", line 2246, in populate\r\n  File \"/tmp/ansible_81XLGz/ansible_modlib.zip/ansible/module_utils/facts.py\", line 2315, in get_memory_facts\r\nAttributeError: 'NoneType' object has no attribute 'groups'\r\n",
"msg": "MODULE FAILURE",
"rc": 0
}

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

    if collected_facts.get("ansible_architecture") in ["9000/800", "9000/785"]:
        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