Repository: ansible/ansible

Issue:
Inaccurate YAML inventory warning introduced by #40660
<!---
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 -->
d7419b4dbc15c53317fe6490146bfd4906bc60d0 from #40660 makes a YAML inventory with an empty group warn that the empty group "is not a valid group name." The warning can be avoided in this case by explicitly using an empty mapping, but I'm not sure what the intent of this change was since the check that triggers the warning has nothing to do with the group name. 

##### 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-->
inventory/yaml

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.7.0dev0 (detached HEAD 20f93816d6) last updated 2018/05/29 19:15:35 (GMT +000)
  config file = None
  configured module search path = [u'/home/zeke/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/zeke/git/ansible/lib/ansible
  executable location = /home/zeke/git/ansible/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
<!--- 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).-->

##### 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.-->
N/A

##### 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 -->

Run `ansible-inventory -i testinv --list`

testinv:
```yaml
all:
  children:
    testgroup:
```


<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
No warning should be produced, and the empty group is added to the inventory.

```
{
    "_meta": {
        "hostvars": {}
    }, 
    "all": {
        "children": [
            "testgroup", 
            "ungrouped"
        ]
    }, 
    "testgroup": {}, 
    "ungrouped": {}
}
```

Alternatively, a warning that the group is not a mapping should be produced, and the group should not be added to the inventory.

```
 [WARNING]: Skipping 'testgroup' since it is not a valid group entry

{
    "_meta": {
        "hostvars": {}
    }, 
    "all": {
        "children": [
            "ungrouped"
        ]
    }, 
    "ungrouped": {}
}
```

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
The group is added to the inventory, but Ansible claims that it was skipped due to an invalid name.

<!--- Paste verbatim command output between quotes below -->
```
 [WARNING]: Skipping 'testgroup' as this is not a valid group name

{
    "_meta": {
        "hostvars": {}
    }, 
    "all": {
        "children": [
            "testgroup", 
            "ungrouped"
        ]
    }, 
    "testgroup": {}, 
    "ungrouped": {}
}
```



Discussion:
Files identified in the description:
* [lib/ansible/parsing/utils/yaml.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/parsing/utils/yaml.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/yaml.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/inventory/yaml.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 --->

Resolving patch:
diff --git a/lib/ansible/plugins/inventory/yaml.py b/lib/ansible/plugins/inventory/yaml.py
--- a/lib/ansible/plugins/inventory/yaml.py
+++ b/lib/ansible/plugins/inventory/yaml.py
@@ -112,9 +112,10 @@ def parse(self, inventory, loader, path, cache=True):
 
     def _parse_group(self, group, group_data):
 
-        self.inventory.add_group(group)
-
         if isinstance(group_data, MutableMapping):
+
+            self.inventory.add_group(group)
+
             # make sure they are dicts
             for section in ['vars', 'children', 'hosts']:
                 if section in group_data:
@@ -144,7 +145,7 @@ def _parse_group(self, group, group_data):
                     self.display.warning('Skipping unexpected key (%s) in group (%s), only "vars", "children" and "hosts" are valid' % (key, group))
 
         else:
-            self.display.warning("Skipping '%s' as this is not a valid group name" % group)
+            self.display.warning("Skipping '%s' as this is not a valid group definition" % group)
 
     def _parse_host(self, host_pattern):
         '''
@@ -164,7 +165,7 @@ def _expand_hostpattern(self, hostpattern):
 
         try:
             (pattern, port) = parse_address(hostpattern, allow_ranges=True)
-        except:
+        except Exception:
             # not a recognizable host pattern
             pattern = hostpattern
             port = None


Test patch: