Repository: ansible/ansible

Issue:
YAML inventory: ungrouped group isn't populated
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
lib/ansible/plugins/inventory/yaml.py

##### ANSIBLE VERSION
```
ansible 2.5.0 (devel 7c187cae93) last updated 2017/12/13 16:21:51 (GMT +200)
```

##### OS / ENVIRONMENT
N/A

##### SUMMARY
When using YAML inventory, [`ungrouped` default group](http://docs.ansible.com/ansible/devel/intro_inventory.html#default-groups) is never populated.

##### 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.
-->

<!--- Paste example playbooks or commands between quotes below -->
`hosts.yaml`:
```yaml
all:
  hosts:
    testhost:
```

##### EXPECTED RESULTS
```
$ ansible-inventory -i hosts.yml  --list
{
    "_meta": {
        "hostvars": {
            "testhost": {}
        }
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    },
    "ungrouped": {
        "hosts": [
            "testhost"
        ]
    }
}
```

```
$ ansible localhost -i hosts.yml -m debug -a 'msg={{ groups }}'
localhost | SUCCESS => {
    "msg": {
        "all": [
            "localhost"
        ],
        "ungrouped": [
            "localhost"
        ]
    }
}
``` 

##### ACTUAL RESULTS

```
$ ansible-inventory -i hosts.yml  --list
```

```
$ ansible-inventory -i /tmp/hosts.yml  --list
{
    "_meta": {
        "hostvars": {
            "localhost": {}
        }
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    },
    "ungrouped": {}
}
```
```
$ ansible localhost -i /tmp/hosts.yml -m debug -a 'msg={{ groups }}'
localhost | SUCCESS => {
    "changed": false,
    "msg": {
        "all": [
            "localhost"
        ],
        "ungrouped": []
    }
}
```

##### RESULT WITH 2.3

Using ansible 2.3 (`ansible 2.3.3.0 (stable-2.3 797d999513) last updated 2017/12/13 17:38:28 (GMT +200)`) `localhost` belongs to `ungrouped`.

```
$ ansible localhost -i hosts.yml -m debug -a 'msg={{ groups }}'
localhost | SUCCESS => {
    "msg": {
        "all": [
            "localhost"
        ],
        "ungrouped": [
            "localhost"
        ]
    }
}
```


Discussion:

Resolving patch:
diff --git a/lib/ansible/inventory/data.py b/lib/ansible/inventory/data.py
--- a/lib/ansible/inventory/data.py
+++ b/lib/ansible/inventory/data.py
@@ -125,10 +125,6 @@ def reconcile_inventory(self):
 
             mygroups = host.get_groups()
 
-            # ensure hosts are always in 'all'
-            if 'all' not in mygroups and not host.implicit:
-                self.add_child('all', host.name)
-
             if self.groups['ungrouped'] in mygroups:
                 # clear ungrouped of any incorrectly stored by parser
                 if set(mygroups).difference(set([self.groups['all'], self.groups['ungrouped']])):
@@ -137,7 +133,7 @@ def reconcile_inventory(self):
             elif not host.implicit:
                 # add ungrouped hosts to ungrouped, except implicit
                 length = len(mygroups)
-                if length == 0 or (length == 1 and all in mygroups):
+                if length == 0 or (length == 1 and self.groups['all'] in mygroups):
                     self.add_child('ungrouped', host.name)
 
             # special case for implicit hosts


Test patch: