# Buggy code
def main():
    module = (
        AnsibleModule(
            argument_spec=dict(
                name=dict(type="list", aliases=["package", "pkg"]),
                state=dict(
                    type="str",
                    default="present",
                    choices=["absent", "installed", "latest", "present", "removed"],
                ),
                recurse=dict(type="bool", default=False),
                force=dict(type="bool", default=False),
                upgrade=dict(type="bool", default=False),
                update_cache=dict(type="bool", default=False, aliases=["update-cache"]),
            ),
            required_one_of=[["name", "update_cache", "upgrade"]],
            supports_check_mode=True,
        ),
    )

    pacman_path = module.get_bin_path("pacman", True)

    p = module.params

    # normalize the state parameter
    if p["state"] in ["present", "installed"]:
        p["state"] = "present"
    elif p["state"] in ["absent", "removed"]:
        p["state"] = "absent"

    if p["update_cache"] and not module.check_mode:
        update_package_db(module, pacman_path)
        if not (p["name"] or p["upgrade"]):
            module.exit_json(changed=True, msg="Updated the package master lists")

    if p["update_cache"] and module.check_mode and not (p["name"] or p["upgrade"]):
        module.exit_json(changed=True, msg="Would have updated the package cache")

    if p["upgrade"]:
        upgrade(module, pacman_path)

    if p["name"]:
        pkgs = expand_package_groups(module, pacman_path, p["name"])

        pkg_files = []
        for i, pkg in enumerate(pkgs):
            if not pkg:  # avoid empty strings
                continue
            elif re.match(".*\.pkg\.tar(\.(gz|bz2|xz|lrz|lzo|Z))?$", pkg):
                # The package given is a filename, extract the raw pkg name from
                # it and store the filename
                pkg_files.append(pkg)
                pkgs[i] = re.sub("-[0-9].*$", "", pkgs[i].split("/")[-1])
            else:
                pkg_files.append(None)

        if module.check_mode:
            check_packages(module, pacman_path, pkgs, p["state"])

        if p["state"] in ["present", "latest"]:
            install_packages(module, pacman_path, p["state"], pkgs, pkg_files)
        elif p["state"] == "absent":
            remove_packages(module, pacman_path, pkgs)


# Traceback (AttributeError)
Using module file /home/thom/git/ansible/lib/ansible/modules/packaging/os/pacman.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: thom
<127.0.0.1> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/thom/.ansible/tmp/ansible-tmp-1510228783.2242217-178302118505177 `" &amp;&amp; echo ansible-tmp-1510228783.2242217-178302118505177="` echo /home/thom/.ansible/tmp/ansible-tmp-1510228783.2242217-178302118505177 `" ) &amp;&amp; sleep 0'
<127.0.0.1> PUT /tmp/tmprmvh2gkm TO /home/thom/.ansible/tmp/ansible-tmp-1510228783.2242217-178302118505177/pacman.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/thom/.ansible/tmp/ansible-tmp-1510228783.2242217-178302118505177/ /home/thom/.ansible/tmp/ansible-tmp-1510228783.2242217-178302118505177/pacman.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'sudo -H -S  -p "[sudo via ansible, key=] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-; /home/thom/.pythonenvs/ansible/bin/python3 /home/thom/.ansible/tmp/ansible-tmp-1510228783.2242217-178302118505177/pacman.py; rm -rf "/home/thom/.ansible/tmp/ansible-tmp-1510228783.2242217-178302118505177/" > /dev/null 2>&amp;1'"'"' &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_6sla8yao/ansible_module_pacman.py", line 437, in <module>
main()
File "/tmp/ansible_6sla8yao/ansible_module_pacman.py", line 391, in main
pacman_path = module.get_bin_path('pacman', True)
AttributeError: 'tuple' object has no attribute 'get_bin_path'

fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_6sla8yao/ansible_module_pacman.py\", line 437, in <module>\n    main()\n  File \"/tmp/ansible_6sla8yao/ansible_module_pacman.py\", line 391, in main\n    pacman_path = module.get_bin_path('pacman', True)\nAttributeError: 'tuple' object has no attribute 'get_bin_path'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type="list", aliases=["package", "pkg"]),
            state=dict(
                type="str",
                default="present",
                choices=["absent", "installed", "latest", "present", "removed"],
            ),
            recurse=dict(type="bool", default=False),
            force=dict(type="bool", default=False),
            upgrade=dict(type="bool", default=False),
            update_cache=dict(type="bool", default=False, aliases=["update-cache"]),
        ),
        required_one_of=[["name", "update_cache", "upgrade"]],
        supports_check_mode=True,
    )

    pacman_path = module.get_bin_path("pacman", True)

    p = module.params

    # normalize the state parameter
    if p["state"] in ["present", "installed"]:
        p["state"] = "present"
    elif p["state"] in ["absent", "removed"]:
        p["state"] = "absent"

    if p["update_cache"] and not module.check_mode:
        update_package_db(module, pacman_path)
        if not (p["name"] or p["upgrade"]):
            module.exit_json(changed=True, msg="Updated the package master lists")

    if p["update_cache"] and module.check_mode and not (p["name"] or p["upgrade"]):
        module.exit_json(changed=True, msg="Would have updated the package cache")

    if p["upgrade"]:
        upgrade(module, pacman_path)

    if p["name"]:
        pkgs = expand_package_groups(module, pacman_path, p["name"])

        pkg_files = []
        for i, pkg in enumerate(pkgs):
            if not pkg:  # avoid empty strings
                continue
            elif re.match(".*\.pkg\.tar(\.(gz|bz2|xz|lrz|lzo|Z))?$", pkg):
                # The package given is a filename, extract the raw pkg name from
                # it and store the filename
                pkg_files.append(pkg)
                pkgs[i] = re.sub("-[0-9].*$", "", pkgs[i].split("/")[-1])
            else:
                pkg_files.append(None)

        if module.check_mode:
            check_packages(module, pacman_path, pkgs, p["state"])

        if p["state"] in ["present", "latest"]:
            install_packages(module, pacman_path, p["state"], pkgs, pkg_files)
        elif p["state"] == "absent":
            remove_packages(module, pacman_path, pkgs)