Repository: ansible/ansible

Issue:
YAML Inventory Parsing Issue
<!---
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
In Ansible 2.6 RC3 we are seeing an issue where the YAML inventory parser is not correctly identifying groups.  Eg, for this inventory file:
```
---
group1:
  children:
    group2:

group2:
  hosts:
    host1:
    host2:
```
Running "ansible group1 --list-hosts" complains about "group2":
```
 [WARNING]: Skipping 'group2' as this is not a valid group definition

 [WARNING]: No hosts matched, nothing to do

  hosts (0):
```


##### 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-->

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0rc3
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /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).-->
```
DEFAULT_HOST_LIST(/etc/ansible/ansible.cfg) = [u'/etc/ansible/hosts.yml']
```
##### 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.-->

CentOS Linux release 7.4.1708 (Core)

##### 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 -->
```yaml
# cat hosts.yml
---
group1:
  children:
    group2:

group2:
  hosts:
    host1:
    host2:


```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->

With Ansible 2.4 and 2.5 we get these results:
```
# ansible group1 --list-hosts
  hosts (2):
    host2
    host1
```
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

With ansible 2.6 we get these results:
```
# ansible group1 --list-hosts -vvv
ansible 2.6.0rc3
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /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)]
Using /etc/ansible/ansible.cfg as config file
 [WARNING]: Skipping 'group2' as this is not a valid group definition

Parsed /etc/ansible/hosts.yml inventory source with ini plugin
 [WARNING]: No hosts matched, nothing to do

  hosts (0):
```
<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:
with patch from PR above I now get:
```
  hosts (2):
    host2
    host1
```
@bcoca I applied the patch and it works as advertised.  Thanks for the quick fix!

Resolving patch:
diff --git a/lib/ansible/cli/inventory.py b/lib/ansible/cli/inventory.py
--- a/lib/ansible/cli/inventory.py
+++ b/lib/ansible/cli/inventory.py
@@ -42,6 +42,7 @@
                            'ansible_playbook_python',
                            'ansible_run_tags',
                            'ansible_skip_tags',
+                           'ansible_verbosity',
                            'ansible_version',
                            'inventory_dir',
                            'inventory_file',
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
@@ -59,6 +59,7 @@
 '''
 
 import os
+
 from collections import MutableMapping
 
 from ansible.errors import AnsibleParserError
@@ -112,37 +113,38 @@ def parse(self, inventory, loader, path, cache=True):
 
     def _parse_group(self, group, group_data):
 
-        if isinstance(group_data, MutableMapping):
+        if isinstance(group_data, (MutableMapping, type(None))):
 
             self.inventory.add_group(group)
 
-            # make sure they are dicts
-            for section in ['vars', 'children', 'hosts']:
-                if section in group_data:
-                    # convert strings to dicts as these are allowed
-                    if isinstance(group_data[section], string_types):
-                        group_data[section] = {group_data[section]: None}
-
-                    if not isinstance(group_data[section], MutableMapping):
-                        raise AnsibleParserError('Invalid "%s" entry for "%s" group, requires a dictionary, found "%s" instead.' %
-                                                 (section, group, type(group_data[section])))
-
-            for key in group_data:
-                if key == 'vars':
-                    for var in group_data['vars']:
-                        self.inventory.set_variable(group, var, group_data['vars'][var])
-
-                elif key == 'children':
-                    for subgroup in group_data['children']:
-                        self._parse_group(subgroup, group_data['children'][subgroup])
-                        self.inventory.add_child(group, subgroup)
-
-                elif key == 'hosts':
-                    for host_pattern in group_data['hosts']:
-                        hosts, port = self._parse_host(host_pattern)
-                        self._populate_host_vars(hosts, group_data['hosts'][host_pattern] or {}, group, port)
-                else:
-                    self.display.warning('Skipping unexpected key (%s) in group (%s), only "vars", "children" and "hosts" are valid' % (key, group))
+            if group_data is not None:
+                # make sure they are dicts
+                for section in ['vars', 'children', 'hosts']:
+                    if section in group_data:
+                        # convert strings to dicts as these are allowed
+                        if isinstance(group_data[section], string_types):
+                            group_data[section] = {group_data[section]: None}
+
+                        if not isinstance(group_data[section], (MutableMapping, type(None))):
+                            raise AnsibleParserError('Invalid "%s" entry for "%s" group, requires a dictionary, found "%s" instead.' %
+                                                     (section, group, type(group_data[section])))
+
+                for key in group_data:
+                    if key == 'vars':
+                        for var in group_data['vars']:
+                            self.inventory.set_variable(group, var, group_data['vars'][var])
+
+                    elif key == 'children':
+                        for subgroup in group_data['children']:
+                            self._parse_group(subgroup, group_data['children'][subgroup])
+                            self.inventory.add_child(group, subgroup)
+
+                    elif key == 'hosts':
+                        for host_pattern in group_data['hosts']:
+                            hosts, port = self._parse_host(host_pattern)
+                            self._populate_host_vars(hosts, group_data['hosts'][host_pattern] or {}, group, port)
+                    else:
+                        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 definition" % group)


Test patch: