Repository: ansible/ansible

Issue:
constructed inventory does not use group variables from previous inventory
##### ISSUE TYPE
 - Feature Idea

##### COMPONENT NAME
constructed

##### ANSIBLE VERSION
```
ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
```

##### CONFIGURATION
N/A

##### OS / ENVIRONMENT
N/A

##### SUMMARY
The constructed inventory plugin doesn't take group variables from previous inventory into account.

##### STEPS TO REPRODUCE
yaml inventory:
```yaml
group:
  hosts:
    host1:
    host2:
  vars:
    enabled: true
```

constructed inventory:
```yaml
plugin: constructed
strict: False

groups:
  group_enabled: enabled | default(false)
```

```
ANSIBLE_INVENTORY_ENABLED=yaml,constructed ansible group_enabled --list-hosts
```

##### EXPECTED RESULTS
I expect output from above ansible command:
```
  hosts (2):
    host1
    host2
```


##### ACTUAL RESULTS
With current version (also the one from git master HEAD) the output is:
```
  hosts (0):
```

##### FEATURE/FIX
Change ansible/plugins/inventory/constructed.py this way.

Add line:
```
from ansible.inventory.helpers import sort_groups, get_group_vars
```

Replace line(s):
```
hostvars = inventory.hosts[host].get_vars()
```
with:
```
hostvars = combine_vars(get_group_vars(inventory.hosts[host].get_groups()), inventory.hosts[host].get_vars())
```


Discussion:
Files identified in the description:
* [lib/ansible/plugins/inventory/constructed.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/inventory/constructed.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 --->
@lufik do you want to submit a PR with the change above?

Resolving patch:
diff --git a/lib/ansible/plugins/inventory/constructed.py b/lib/ansible/plugins/inventory/constructed.py
--- a/lib/ansible/plugins/inventory/constructed.py
+++ b/lib/ansible/plugins/inventory/constructed.py
@@ -56,6 +56,7 @@
 
 from ansible import constants as C
 from ansible.errors import AnsibleParserError
+from ansible.inventory.helpers import get_group_vars
 from ansible.plugins.cache import FactCache
 from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
 from ansible.module_utils._text import to_native
@@ -98,7 +99,7 @@ def parse(self, inventory, loader, path, cache=False):
             for host in inventory.hosts:
 
                 # get available variables to templar
-                hostvars = inventory.hosts[host].get_vars()
+                hostvars = combine_vars(get_group_vars(inventory.hosts[host].get_groups()), inventory.hosts[host].get_vars())
                 if host in fact_cache:  # adds facts if cache is active
                     hostvars = combine_vars(hostvars, fact_cache[host])
 
@@ -106,7 +107,7 @@ def parse(self, inventory, loader, path, cache=False):
                 self._set_composite_vars(self.get_option('compose'), hostvars, host, strict=strict)
 
                 # refetch host vars in case new ones have been created above
-                hostvars = inventory.hosts[host].get_vars()
+                hostvars = combine_vars(get_group_vars(inventory.hosts[host].get_groups()), inventory.hosts[host].get_vars())
                 if host in self._cache:  # adds facts if cache is active
                     hostvars = combine_vars(hostvars, self._cache[host])
 


Test patch: