# Buggy code
def main():
    """The main function."""
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(aliases=["pkg"], type="list"),
            state=dict(
                default="installed",
                choices=["absent", "present", "installed", "removed", "latest"],
            ),
            enablerepo=dict(type="list", default=[]),
            disablerepo=dict(type="list", default=[]),
            list=dict(),
            conf_file=dict(default=None, type="path"),
            disable_gpg_check=dict(default=False, type="bool"),
        ),
        required_one_of=[["name", "list"]],
        mutually_exclusive=[["name", "list"]],
        supports_check_mode=True,
    )
    params = module.params

    _fail_if_no_dnf(module)
    if params["list"]:
        base = _base(
            module,
            params["conf_file"],
            params["disable_gpg_check"],
            params["disablerepo"],
            params["enablerepo"],
        )
        list_items(module, base, params["list"])
    else:
        # Note: base takes a long time to run so we want to check for failure
        # before running it.
        if not util.am_i_root():
            module.fail_json(msg="This command has to be run under the root user.")
        base = _base(
            module,
            params["conf_file"],
            params["disable_gpg_check"],
            params["disablerepo"],
            params["enablerepo"],
        )

        ensure(module, base, params["state"], params["name"])


# Traceback (nNameError)
TASK [install essential software] *********************************************
failed: [54.213.90.47] => (item=[u'python-dnf', u'java-1.8.0-openjdk']) => {"failed": true, "item": ["python-dnf", "java-1.8.0-openjdk"], "module_stderr": "", "module_stdout": "Traceback (most recent call last):\r\n  File \"/home/fedora/.ansible/tmp/ansible-tmp-1455147718.94-110285313538093/dnf\", line 2284, in <module>\r\n    main()\r\n  File \"/home/fedora/.ansible/tmp/ansible-tmp-1455147718.94-110285313538093/dnf\", line 339, in main\r\n    if not util.am_i_root():\r\nNameError: global name 'util' is not defined\r\n", "msg": "MODULE FAILURE", "parsed": false}

# Fixed code
def main():
    """The main function."""
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(aliases=["pkg"], type="list"),
            state=dict(
                default="installed",
                choices=["absent", "present", "installed", "removed", "latest"],
            ),
            enablerepo=dict(type="list", default=[]),
            disablerepo=dict(type="list", default=[]),
            list=dict(),
            conf_file=dict(default=None, type="path"),
            disable_gpg_check=dict(default=False, type="bool"),
        ),
        required_one_of=[["name", "list"]],
        mutually_exclusive=[["name", "list"]],
        supports_check_mode=True,
    )
    params = module.params

    _ensure_dnf(module)

    if params["list"]:
        base = _base(
            module,
            params["conf_file"],
            params["disable_gpg_check"],
            params["disablerepo"],
            params["enablerepo"],
        )
        list_items(module, base, params["list"])
    else:
        # Note: base takes a long time to run so we want to check for failure
        # before running it.
        if not dnf.util.am_i_root():
            module.fail_json(msg="This command has to be run under the root user.")
        base = _base(
            module,
            params["conf_file"],
            params["disable_gpg_check"],
            params["disablerepo"],
            params["enablerepo"],
        )

        ensure(module, base, params["state"], params["name"])