import tempfile


def build_ansible_inventory(config=None, fw_ip=None):
    """
    Build a basic ini-style Ansible inventory file.

    Parameters:
        config (dict): Optional dictionary representing the current configuration file (used in production mode).
        fw_ip (string): Optional firewall IP address (used in test mode).

    Returns:
        file object: File descriptor of the file containing the Ansible inventory.
    """

    inv = tempfile.NamedTemporaryFile(mode="w", delete=False)
    if fw_ip:
        inv.write(fw_ip + "\n")
    else:
        if not config or "production" not in config or "firewalls" not in config["production"]:
            raise Exception("The configuration must include a production.firewalls section")

        for fw in config["production"]["firewalls"]:
            inv.write(fw + "\n")

    inv.close()

    return inv