# Buggy code
def validate_host_filter(self, host_filter):
    if host_filter:
        try:
            for match in JSONBField.get_lookups().keys():
                if match == "exact":
                    # __exact is allowed
                    continue
                match = "__{}".format(match)
                if re.match("ansible_facts[^=]+{}=".format(match), host_filter):
                    raise models.base.ValidationError(
                        {
                            "host_filter": "ansible_facts does not support searching with {}".format(
                                match
                            )
                        }
                    )
            SmartFilter().query_from_string(host_filter)
        except RuntimeError as e:
            raise models.base.ValidationError(e)
    return host_filter


# Traceback (tower_cli.exceptions.ServerError)
The full traceback is:
Traceback (most recent call last):
File "<stdin>", line 102, in <module>
File "<stdin>", line 94, in _ansiballz_main
File "<stdin>", line 40, in invoke_module
File "/usr/lib64/python2.7/runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "/usr/lib64/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "/usr/lib64/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/tmp/ansible_tower_inventory_payload_Ptt6kB/ansible_tower_inventory_payload.zip/ansible/modules/web_infrastructure/ansible_tower/tower_inventory.py", line 130, in <module>
File "/tmp/ansible_tower_inventory_payload_Ptt6kB/ansible_tower_inventory_payload.zip/ansible/modules/web_infrastructure/ansible_tower/tower_inventory.py", line 116, in main
File "/usr/lib/python2.7/site-packages/tower_cli/models/base.py", line 720, in modify
return self.write(pk, create_on_missing=create_on_missing, force_on_exists=True, **kwargs)
File "/usr/lib/python2.7/site-packages/tower_cli/models/base.py", line 424, in write
r = getattr(client, method.lower())(url, data=kwargs)
File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 578, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "/usr/lib/python2.7/site-packages/tower_cli/api.py", line 262, in request
raise exc.ServerError('The Tower server sent back a server error. '
tower_cli.exceptions.ServerError: The Tower server sent back a server error. Please try again later.

failed: [dhcp-3-213.vms.sat.rdu2.redhat.com] (item={'name': 'Test Inventory - Smart', 'organization': 'Default', 'description': 'created by Ansible Playbook', 'state': 'present', 'kind': 'smart', 'host_filter': 'ansible'}) => {
"ansible_loop_var": "item",
"changed": false,
"item": {
"description": "created by Ansible Playbook",
"host_filter": "ansible",
"kind": "smart",
"name": "Test Inventory - Smart",
"organization": "Default",
"state": "present"
},
"module_stderr": "Traceback (most recent call last):\n  File \"<stdin>\", line 102, in <module>\n  File \"<stdin>\", line 94, in _ansiballz_main\n  File \"<stdin>\", line 40, in invoke_module\n  File \"/usr/lib64/python2.7/runpy.py\", line 176, in run_module\n    fname, loader, pkg_name)\n  File \"/usr/lib64/python2.7/runpy.py\", line 82, in _run_module_code\n    mod_name, mod_fname, mod_loader, pkg_name)\n  File \"/usr/lib64/python2.7/runpy.py\", line 72, in _run_code\n    exec code in run_globals\n  File \"/tmp/ansible_tower_inventory_payload_Ptt6kB/ansible_tower_inventory_payload.zip/ansible/modules/web_infrastructure/ansible_tower/tower_inventory.py\", line 130, in <module>\n  File \"/tmp/ansible_tower_inventory_payload_Ptt6kB/ansible_tower_inventory_payload.zip/ansible/modules/web_infrastructure/ansible_tower/tower_inventory.py\", line 116, in main\n  File \"/usr/lib/python2.7/site-packages/tower_cli/models/base.py\", line 720, in modify\n    return self.write(pk, create_on_missing=create_on_missing, force_on_exists=True, **kwargs)\n  File \"/usr/lib/python2.7/site-packages/tower_cli/models/base.py\", line 424, in write\n    r = getattr(client, method.lower())(url, data=kwargs)\n  File \"/usr/lib/python2.7/site-packages/requests/sessions.py\", line 578, in post\n    return self.request('POST', url, data=data, json=json, **kwargs)\n  File \"/usr/lib/python2.7/site-packages/tower_cli/api.py\", line 262, in request\n    raise exc.ServerError('The Tower server sent back a server error. '\ntower_cli.exceptions.ServerError: The Tower server sent back a server error. Please try again later.\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}

# Fixed code
def validate_host_filter(self, host_filter):
    if host_filter:
        try:
            for match in JSONBField.get_lookups().keys():
                if match == "exact":
                    # __exact is allowed
                    continue
                match = "__{}".format(match)
                if re.match("ansible_facts[^=]+{}=".format(match), host_filter):
                    raise models.base.ValidationError(
                        {
                            "host_filter": "ansible_facts does not support searching with {}".format(
                                match
                            )
                        }
                    )
            SmartFilter().query_from_string(host_filter)
        except RuntimeError as e:
            raise models.base.ValidationError(str(e))
    return host_filter