# Buggy code
def get_groups_from_server(server_vars, namegroup=True):
    groups = []

    region = server_vars["region"]
    cloud = server_vars["cloud"]
    metadata = server_vars.get("metadata", {})

    # Create a group for the cloud
    groups.append(cloud)

    # Create a group on region
    groups.append(region)

    # And one by cloud_region
    groups.append("%s_%s" % (cloud, region))

    # Check if group metadata key in servers' metadata
    if "group" in metadata:
        groups.append(metadata["group"])

    for extra_group in metadata.get("groups", "").split(","):
        if extra_group:
            groups.append(extra_group.strip())

    groups.append("instance-%s" % server_vars["id"])
    if namegroup:
        groups.append(server_vars["name"])

    for key in ("flavor", "image"):
        if "name" in server_vars[key]:
            groups.append("%s-%s" % (key, server_vars[key]["name"]))

    for key, value in iter(metadata.items()):
        groups.append("meta-%s_%s" % (key, value))

    az = server_vars.get("az", None)
    if az:
        # Make groups for az, region_az and cloud_region_az
        groups.append(az)
        groups.append("%s_%s" % (region, az))
        groups.append("%s_%s_%s" % (cloud, region, az))
    return groups


# Traceback (RuntimeError)
2.890 INFO     Updating inventory 722: Inventory - WindowMonitor㚝
2.916 INFO     Reading Ansible inventory source: /awx_devel/awx/plugins/inventory/openstack.py
Traceback (most recent call last):
File "/usr/local/bin/awx-manage", line 9, in <module>
load_entry_point('awx', 'console_scripts', 'awx-manage')()
File "/awx_devel/awx/__init__.py", line 116, in manage
execute_from_command_line(sys.argv)
File "/venv/awx/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/venv/awx/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/venv/awx/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/venv/awx/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/awx_devel/awx/main/management/commands/inventory_import.py", line 994, in handle
self.is_custom)
File "/awx_devel/awx/main/management/commands/inventory_import.py", line 245, in load_inventory_source
is_custom=is_custom).load()
File "/awx_devel/awx/main/management/commands/inventory_import.py", line 180, in load
data = self.command_to_json(base_args + ['--list'])
File "/awx_devel/awx/main/management/commands/inventory_import.py", line 163, in command_to_json
self.method, proc.returncode, stdout, stderr))
RuntimeError: ansible-inventory failed (rc=1) with stdout:

stderr:
[WARNING]:  * Failed to parse /awx_devel/awx/plugins/inventory/openstack.py
with script plugin: Invalid empty/false group name provided:

[WARNING]: Unable to parse /awx_devel/awx/plugins/inventory/openstack.py as an
inventory source

ERROR! No inventory was parsed, please check your configuration and options.

# Fixed code
def get_groups_from_server(server_vars, namegroup=True):
    groups = []

    region = server_vars["region"]
    cloud = server_vars["cloud"]
    metadata = server_vars.get("metadata", {})

    # Create a group for the cloud
    groups.append(cloud)

    # Create a group on region
    if region:
        groups.append(region)

    # And one by cloud_region
    groups.append("%s_%s" % (cloud, region))

    # Check if group metadata key in servers' metadata
    if "group" in metadata:
        groups.append(metadata["group"])

    for extra_group in metadata.get("groups", "").split(","):
        if extra_group:
            groups.append(extra_group.strip())

    groups.append("instance-%s" % server_vars["id"])
    if namegroup:
        groups.append(server_vars["name"])

    for key in ("flavor", "image"):
        if "name" in server_vars[key]:
            groups.append("%s-%s" % (key, server_vars[key]["name"]))

    for key, value in iter(metadata.items()):
        groups.append("meta-%s_%s" % (key, value))

    az = server_vars.get("az", None)
    if az:
        # Make groups for az, region_az and cloud_region_az
        groups.append(az)
        groups.append("%s_%s" % (region, az))
        groups.append("%s_%s_%s" % (cloud, region, az))
    return groups