Repository: ansible/ansible

Issue:
YAML inventory - Expected key=value host variable assignment
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Documentation Report
 - Maybe bug?

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
Ansible Inventory YAML

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.3.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
```

##### CONFIGURATION
Vanilla ansible.cfg, no changes


##### OS / ENVIRONMENT
<!---
Mention 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.
-->
Ubuntu 16.04 to -> Proxmox Ubuntu 16.04 LXC

##### SUMMARY
<!--- Explain the problem briefly -->
Following the documentation for YAML inventories, the ansible-playbook errors on on the `vars:` syntax. Saying to use `ansible_user=root` instead of the documented `ansible_user: root` format

https://github.com/ansible/ansible/blob/devel/examples/hosts.yaml

##### 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 -->
Inventory:
```yaml
all:
  hosts: 
  children: 
    world0: 
      hosts: 
        git.server.com: 
        gui.server.com: 
        keras.server.com: 
        ldap.server.com: 
        awx.server.com: 
        radius.server.com: 
      vars:
        ansible_user: root

```

Playbook:
```
---
# https://gist.github.com/gwillem/4ba393dceb55e5ae276a87300f6b8e6f
- hosts: world0

  tasks:
  - name: install python 2
    raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
```
<!--- You can also paste gist.github.com links for larger files -->

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
eric@ainiml:~/Desktop/ansible$ ansible-playbook -i host-yaml /home/eric/Desktop/github/antsable-tsl/playbooks/python-minimal.yml 
ERROR! Attempted to read "host-yaml" as YAML: 'NoneType' object is not iterable
Attempted to read "host-yaml" as ini file: host-yaml:13: Expected key=value host variable assignment, got: root 
```



Discussion:
Actually, you get 2 errors, 1 when it tried to read as YAML, since that failed, it also tried to read as ini file.

This is the error from the YAML parser .. though not very helpful.
`ERROR! Attempted to read "host-yaml" as YAML: 'NoneType' object is not iterable`

I tested the file posted above and that seems to work fine with 2.4, testing with 2.3
hrmphf, 2.3 does not like the empty 'hosts:' entry, this should work for you:
```yaml
all:
  children: 
    world0: 
      hosts: 
        git.server.com: 
        gui.server.com: 
        keras.server.com: 
        ldap.server.com: 
        awx.server.com: 
        radius.server.com: 
      vars:
        ansible_user: root
```
run with 2.3
```
#> ansible -m debug -a 'var=inventory_hostname' -i ./inv.yml all
keras.server.com | SUCCESS => {
    "inventory_hostname": "keras.server.com"
}
gui.server.com | SUCCESS => {
    "inventory_hostname": "gui.server.com"
}
ldap.server.com | SUCCESS => {
    "inventory_hostname": "ldap.server.com"
}
git.server.com | SUCCESS => {
    "inventory_hostname": "git.server.com"
}
radius.server.com | SUCCESS => {
    "inventory_hostname": "radius.server.com"
}
awx.server.com | SUCCESS => {
    "inventory_hostname": "awx.server.com"
}
```

Resolving patch:
diff --git a/lib/ansible/inventory/yaml.py b/lib/ansible/inventory/yaml.py
--- a/lib/ansible/inventory/yaml.py
+++ b/lib/ansible/inventory/yaml.py
@@ -83,19 +83,19 @@ def _parse_groups(self, group, group_data):
                 if section in group_data and isinstance(group_data[section], string_types):
                     group_data[section] = { group_data[section]: None}
 
-            if 'vars' in group_data:
+            if group_data.get('vars', False):
                 for var in group_data['vars']:
                     if var != 'ansible_group_priority':
                         self.groups[group].set_variable(var, group_data['vars'][var])
                     else:
                         self.groups[group].set_priority(group_data['vars'][var])
 
-            if 'children' in group_data:
+            if group_data.get('children', False):
                 for subgroup in group_data['children']:
                     self._parse_groups(subgroup, group_data['children'][subgroup])
                     self.groups[group].add_child_group(self.groups[subgroup])
 
-            if 'hosts' in group_data:
+            if group_data.get('hosts', False):
                 for host_pattern in group_data['hosts']:
                     hosts = self._parse_host(host_pattern, group_data['hosts'][host_pattern])
                     for h in hosts:


Test patch: