Repository: ansible/ansible

Issue:
ansible-inventory returns unexpected host variable `ansible_inventory_sources`
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME

`ansible-inventory` CLI, found at https://github.com/ansible/ansible/blob/devel/lib/ansible/cli/inventory.py

##### ANSIBLE VERSION
```
$ ansible --version
ansible 2.5.0
  config file = None
  configured module search path = [u'/Users/alancoding/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/alancoding/.virtualenvs/ansible/lib/python2.7/site-packages/ansible-2.5.0-py2.7.egg/ansible
  executable location = /Users/alancoding/.virtualenvs/ansible/bin/ansible
  python version = 2.7.10 (default, Jul 13 2015, 12:05:58) [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
```

##### CONFIGURATION
```
$ ansible-config dump --only-changed
$ 
```
(no output)

##### OS / ENVIRONMENT
OS you are running Ansible from: Mac
OS you are managing: None

##### SUMMARY
Output of `ansible-inventory` has an extra unexpected hostvar (`ansible_inventory_sources`) inside of the keys "_meta", "hostvars", and the host name.

This seems to be a result of the recent introduction of this variable in 2.5.

https://github.com/ansible/ansible/blob/118d2cda249ed6a72c2aa3dc4317b94dc0c0bad8/CHANGELOG.md#L20

##### STEPS TO REPRODUCE

Create a file named `foobar`, containing the following contents

```
foobar
```

Example: https://github.com/AlanCoding/Ansible-inventory-file-examples/blob/master/issues/foobar

Run `ansible-inventory` to get JSON representation of this inventory.

`ansible-inventory -i foobar --list`


##### EXPECTED RESULTS
```
$ ansible-inventory -i foobar --list
{
    "_meta": {
        "hostvars": {
            "foobar": {}
        }
    }, 
    "all": {
        "children": [
            "ungrouped"
        ]
    }, 
    "ungrouped": {
        "hosts": [
            "foobar"
        ]
    }
}
```

##### ACTUAL RESULTS
```
$ ansible-inventory -i foobar --list
{
    "_meta": {
        "hostvars": {
            "foobar": {
                "ansible_inventory_sources": [
                    "/Users/alancoding/Documents/repos/ansible-inventory-file-examples/issues/foobar"
                ]
            }
        }
    }, 
    "all": {
        "children": [
            "ungrouped"
        ]
    }, 
    "ungrouped": {
        "hosts": [
            "foobar"
        ]
    }
}

```

Filing as a bug to keep separate from #30877.

I would generally expect that magic variables are not reflected in the `ansible-inventory` output, because magic variables will still be available without having to provide them in the inventory file.


Discussion:

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
@@ -30,8 +30,15 @@
     from ansible.utils.display import Display
     display = Display()
 
-INTERNAL_VARS = frozenset(['ansible_facts', 'ansible_version',
+INTERNAL_VARS = frozenset(['ansible_diff_mode',
+                           'ansible_facts',
+                           'ansible_forks',
+                           'ansible_inventory_sources',
+                           'ansible_limit',
                            'ansible_playbook_python',
+                           'ansible_run_tags',
+                           'ansible_skip_tags',
+                           'ansible_version',
                            'inventory_dir',
                            'inventory_file',
                            'inventory_hostname',


Test patch: