Repository: ansible/ansible

Issue:
AttributeError: 'Display' object has no attribute 'warn'
The YAML inventory plugin makes a non-existing function call display.warn() when a YAML inventory file contains a non-standard key

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
inventory

##### ANSIBLE VERSION
ansible 2.5.0
  config file = /mnt/c/Users/user/Projects/ansible/ansible.cfg
  configured module search path = [u'/home/zanglang/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

##### CONFIGURATION
N/A

##### OS / ENVIRONMENT
N/A

##### SUMMARY
I am currently writing an inventory plugin based on top of the existing YAML plugin, on the current devel branch. When using a standard YAML inventory format with a non-standard top-level key, Ansible throws an exception complaining about a missing warn() function.

Looking at ansible/utils/display.py looks like this function does not exist.

##### STEPS TO REPRODUCE
Using the follow inventory file: https://gist.github.com/zanglang/aa000162779d976e7d2fbbc0cc6f75e2 run: ansible -i inventory all --list-hosts -vvv

##### EXPECTED RESULTS
Command does not fail.

##### ACTUAL RESULTS

Ansible throws an exception:

> Traceback (most recent call last):
>   File "/usr/bin/ansible", line 106, in <module>
>     exit_code = cli.run()
>   File "/usr/local/lib/python2.7/dist-packages/ansible/cli/adhoc.py", line 111, in run
>     loader, inventory, variable_manager = self._play_prereqs(self.options)
>   File "/usr/local/lib/python2.7/dist-packages/ansible/cli/__init__.py", line 791, in _play_prereqs
>     inventory = InventoryManager(loader=loader, sources=options.inventory)
>   File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 144, in __init__
>     self.parse_sources()
>   File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 205, in parse_sources
>     parse = self.parse_source(source, cache=cache)
>   File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 236, in parse_source
>     parsed_this_one = self.parse_source(to_native(fullpath))
>   File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 236, in parse_source
>     parsed_this_one = self.parse_source(to_native(fullpath))
>   File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 259, in parse_source
>     plugin.parse(self._inventory, self._loader, source, cache=cache)
>   File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/inventory/yaml.py", line 101, in parse
>     self._parse_group(group_name, data[group_name])
>   File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/inventory/yaml.py", line 136, in _parse_group
>     self.display.warn('Skipping unexpected key (%s) in group (%s), only "vars", "children" and "hosts" are valid' % (key, group))
> AttributeError: 'Display' object has no attribute 'warn'`

AttributeError: 'Display' object has no attribute 'warn'
The YAML inventory plugin makes a non-existing function call display.warn() when a YAML inventory file contains a non-standard key

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
inventory

##### ANSIBLE VERSION
ansible 2.5.0
  config file = /mnt/c/Users/user/Projects/ansible/ansible.cfg
  configured module search path = [u'/home/zanglang/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

##### CONFIGURATION
N/A

##### OS / ENVIRONMENT
N/A

##### SUMMARY
I am currently writing an inventory plugin based on top of the existing YAML plugin, on the current devel branch. When using a standard YAML inventory format with a non-standard top-level key, Ansible throws an exception complaining about a missing warn() function.

Looking at ansible/utils/display.py looks like this function does not exist.

##### STEPS TO REPRODUCE
Using the follow inventory file: https://gist.github.com/zanglang/aa000162779d976e7d2fbbc0cc6f75e2 run: ansible -i inventory all --list-hosts -vvv

##### EXPECTED RESULTS
Command does not fail.

##### ACTUAL RESULTS

Ansible throws an exception:

> Traceback (most recent call last):
>   File "/usr/bin/ansible", line 106, in <module>
>     exit_code = cli.run()
>   File "/usr/local/lib/python2.7/dist-packages/ansible/cli/adhoc.py", line 111, in run
>     loader, inventory, variable_manager = self._play_prereqs(self.options)
>   File "/usr/local/lib/python2.7/dist-packages/ansible/cli/__init__.py", line 791, in _play_prereqs
>     inventory = InventoryManager(loader=loader, sources=options.inventory)
>   File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 144, in __init__
>     self.parse_sources()
>   File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 205, in parse_sources
>     parse = self.parse_source(source, cache=cache)
>   File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 236, in parse_source
>     parsed_this_one = self.parse_source(to_native(fullpath))
>   File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 236, in parse_source
>     parsed_this_one = self.parse_source(to_native(fullpath))
>   File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 259, in parse_source
>     plugin.parse(self._inventory, self._loader, source, cache=cache)
>   File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/inventory/yaml.py", line 101, in parse
>     self._parse_group(group_name, data[group_name])
>   File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/inventory/yaml.py", line 136, in _parse_group
>     self.display.warn('Skipping unexpected key (%s) in group (%s), only "vars", "children" and "hosts" are valid' % (key, group))
> AttributeError: 'Display' object has no attribute 'warn'`



Discussion:

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
@@ -133,7 +133,7 @@ def _parse_group(self, group, group_data):
                         hosts, port = self._parse_host(host_pattern)
                         self.populate_host_vars(hosts, group_data['hosts'][host_pattern] or {}, group, port)
                 else:
-                    self.display.warn('Skipping unexpected key (%s) in group (%s), only "vars", "children" and "hosts" are valid' % (key, group))
+                    self.display.warning('Skipping unexpected key (%s) in group (%s), only "vars", "children" and "hosts" are valid' % (key, group))
 
     def _parse_host(self, host_pattern):
         '''


Test patch: