Repository: ansible/ansible

Issue:
OpenStack dynamic inventory host gets variables from a group to which it does not belongs.
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->
During inventory generation by dynamic inventory plugin host gets variables from all groups not only from groups it belongs.

For example I have two openstack instances (FirstServer, SecondServer)

The FirstServer belongs to "linux" group and SecondServer belongs to "windows" group.

group_vars directory have linux.yml and window.yml file for specific variables for groups. 

During ansible-inventory --list (and --export) run I have found that all variables are merged and I have variables from windows group for linux host and visa versa.

The first server have metadata key "groups" with value "stand, linux". The second server have metadata key "groups" with value "stand, windows". So, I have two groups linux and windows.

I have created example project to demonstrate the issue:

https://github.com/ITD27M01/ansible-openstack-bug


The output for "ansible-inventory --list" is: https://github.com/ITD27M01/ansible-openstack-bug/blob/master/openstack-inventory.json#L126
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
`contrib/inventory/openstack_inventory.py`
##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.5
  config file = /Users/igor.tiunov/Sources/ansible-openstack-bug/ansible.cfg
  configured module search path = [u'/Users/igor.tiunov/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.14 (default, Mar  9 2018, 23:57:12) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->
```
DEFAULT_HOST_LIST(/Users/igor.tiunov/Sources/ansible-openstack-bug/ansible.cfg) = [u'/Users/igor.tiunov/Sources/ansible-openstack-bug/openstack.yml']
DEFAULT_VAULT_PASSWORD_FILE(env: ANSIBLE_VAULT_PASSWORD_FILE) = /Users/igor.tiunov/tmp/.vault
INVENTORY_ENABLED(/Users/igor.tiunov/Sources/ansible-openstack-bug/ansible.cfg) = ['openstack']
```
##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
Max OSX and rhel/windows instances on openstack
##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

1. Create openstack instances with groups metadata.
2. Create group_vars directory with groups related files and variables,
3. Use openstack inventory plugin for dynamic inventory
4. Read the ansible-inventory --list ouput or ansible-inventory --list --export

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- name: Try to ping linux hosts
  hosts: linux
  gather_facts: no
  tasks:
    - ping:
...
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Hosts have variables only from their groups.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Host gets variables from a group to which it does not belong.


openstack inventory plugin can create group with an 'empty name'  when region is not specified in clouds.yml
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->
When region_name is not specified the openstack inventory plugin create empty named group because plugin have not any check for an empty value:

https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/inventory/openstack.py#L257

as soon as 'cloud_' group also created:

https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/inventory/openstack.py#L260 

the empty group can lead to unexpected behavior:
https://github.com/ansible/ansible/issues/42040
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
`contrib/inventory/openstack_inventory.py`
##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.5
  config file = /Users/igor.tiunov/Sources/ansible-openstack-bug/ansible.cfg
  configured module search path = [u'/Users/igor.tiunov/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.14 (default, Mar  9 2018, 23:57:12) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->
```
DEFAULT_HOST_LIST(/Users/igor.tiunov/Sources/ansible-openstack-bug/ansible.cfg) = [u'/Users/igor.tiunov/Sources/ansible-openstack-bug/openstack.yml']
DEFAULT_VAULT_PASSWORD_FILE(env: ANSIBLE_VAULT_PASSWORD_FILE) = /Users/igor.tiunov/tmp/.vault
INVENTORY_ENABLED(/Users/igor.tiunov/Sources/ansible-openstack-bug/ansible.cfg) = ['openstack']
```
##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
Max OSX and rhel/windows instances on openstack
##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->
1. Create clouds.yaml like this:
https://github.com/ITD27M01/ansible-openstack-bug/blob/master/clouds.yaml
https://docs.openstack.org/os-client-config/latest/user/configuration.html
2. Run ansible-invetnory --list
3. See the empty group in inventory:
https://github.com/ITD27M01/ansible-openstack-bug/blob/master/openstack-inventory.json#L2
https://github.com/ITD27M01/ansible-openstack-bug/blob/master/openstack-inventory.json#L260
<!--- Paste example playbooks or commands between quotes below -->
```yaml

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Empty group is not created
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Empty group is created
<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:
Files identified in the description:
* [lib/ansible/cli/inventory.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/cli/inventory.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
Files identified in the description:
* [contrib/inventory/openstack_inventory.py](https://github.com/ansible/ansible/blob/devel/contrib/inventory/openstack_inventory.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
Files identified in the description:
* [lib/ansible/plugins/inventory/openstack.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/inventory/openstack.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @dagnello @emonty @juliakreger @omgjlk @rcarrillocruz @shrews @thingee
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Files identified in the description:
* [contrib/inventory/openstack_inventory.py](https://github.com/ansible/ansible/blob/devel/contrib/inventory/openstack_inventory.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @emonty @gtema 
@ITD27M01 , please verify if this fixes your problem. In positive case I will create followup PRs for 2.6 and 2.7/devel
@gtema 

Can you please check the following also:

https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/inventory/openstack.py#L264
https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/inventory/openstack.py#L289


I personally do not think it is a good idea, since that might break group parsing scripts/playbooks by people relying on that structure
In this way the groups like \_nova, cloud_\_nova and cloud_ will be created:

https://github.com/ITD27M01/ansible-openstack-bug/blob/master/openstack-inventory.json#L239-242

Resolving patch:
diff --git a/lib/ansible/plugins/inventory/openstack.py b/lib/ansible/plugins/inventory/openstack.py
--- a/lib/ansible/plugins/inventory/openstack.py
+++ b/lib/ansible/plugins/inventory/openstack.py
@@ -258,7 +258,8 @@ def _get_groups_from_server(self, server_vars, namegroup=True):
         groups.append(cloud)
 
         # Create a group on region
-        groups.append(region)
+        if region:
+            groups.append(region)
 
         # And one by cloud_region
         groups.append("%s_%s" % (cloud, region))


Test patch: