{"action":"created","issue":{"url":"https://api.github.com/repos/ansible/ansible/issues/36177","repository_url":"https://api.github.com/repos/ansible/ansible","labels_url":"https://api.github.com/repos/ansible/ansible/issues/36177/labels{/name}","comments_url":"https://api.github.com/repos/ansible/ansible/issues/36177/comments","events_url":"https://api.github.com/repos/ansible/ansible/issues/36177/events","html_url":"https://github.com/ansible/ansible/issues/36177","id":297124852,"number":36177,"title":"Empty groups from vars plugins inconsistently shown","user":{"login":"AlanCoding","id":1385596,"avatar_url":"https://avatars3.githubusercontent.com/u/1385596?v=4","gravatar_id":"","url":"https://api.github.com/users/AlanCoding","html_url":"https://github.com/AlanCoding","followers_url":"https://api.github.com/users/AlanCoding/followers","following_url":"https://api.github.com/users/AlanCoding/following{/other_user}","gists_url":"https://api.github.com/users/AlanCoding/gists{/gist_id}","starred_url":"https://api.github.com/users/AlanCoding/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlanCoding/subscriptions","organizations_url":"https://api.github.com/users/AlanCoding/orgs","repos_url":"https://api.github.com/users/AlanCoding/repos","events_url":"https://api.github.com/users/AlanCoding/events{/privacy}","received_events_url":"https://api.github.com/users/AlanCoding/received_events","type":"User","site_admin":false},"labels":[{"id":686214011,"url":"https://api.github.com/repos/ansible/ansible/labels/affects_2.5","name":"affects_2.5","color":"ededed","default":false},{"id":48276055,"url":"https://api.github.com/repos/ansible/ansible/labels/bug_report","name":"bug_report","color":"fbca04","default":false},{"id":636352796,"url":"https://api.github.com/repos/ansible/ansible/labels/support:core","name":"support:core","color":"ededed","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-02-14T14:55:21Z","updated_at":"2018-02-20T14:10:38Z","closed_at":"2018-02-20T14:10:38Z","author_association":"MEMBER","body":"##### ISSUE TYPE\r\n - Bug Report\r\n\r\n##### COMPONENT NAME\r\nhost_group_vars Vars Plugins\r\n\r\n##### ANSIBLE VERSION\r\n```\r\n$ ansible --version\r\nansible 2.5.0\r\n  config file = None\r\n  configured module search path = [u'/Users/alancoding/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']\r\n  ansible python module location = /Users/alancoding/.virtualenvs/ansible/lib/python2.7/site-packages/ansible-2.5.0-py2.7.egg/ansible\r\n  executable location = /Users/alancoding/.virtualenvs/ansible/bin/ansible\r\n  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)]\r\n```\r\n\r\n##### CONFIGURATION\r\n```\r\n$ ansible-config dump --only-changed\r\n```\r\n(no output)\r\n\r\n##### OS / ENVIRONMENT\r\nN/A\r\n\r\n##### SUMMARY\r\nA group which is only defined by the `group_vars/` folder next to the inventory file will not show up\r\n - it will not show up in the playbook var `groups`\r\n - it will not show up in the output of `ansible-inventory`\r\n\r\nSubsequently using `add_host`, placing a host inside of this group _will_ cause the group to show up. Variables for the group will subsequently show up in hostvars for the added host.\r\n\r\nThis breaks the portability of `ansible-inventory`.\r\n\r\nThis is a re-filing of a narrow part of the AWX issue filed at https://github.com/ansible/awx/issues/1192\r\n\r\n##### STEPS TO REPRODUCE\r\nFor the inventory file, use the file `just_host.py` inside of the directory in the following repo. This is referenced from the repo root, that being `scripts/var_files/just_host.py`.\r\n\r\nhttps://github.com/AlanCoding/Ansible-inventory-file-examples/tree/master/scripts/var_files\r\n\r\nThe `group_vars/` directly has a file named `ghost`.\r\n\r\n```\r\n$ tree scripts/var_files/\r\nscripts/var_files/\r\n├── group_vars\r\n│   └── ghost\r\n├── host_in_group.py\r\n└── just_host.py\r\n```\r\n\r\nEither `--list` the contents with `ansible-inventory` or echo the `groups` variable in a playbook.\r\n\r\n##### EXPECTED RESULTS\r\nAs a counter-example to set expectations, see alternative (from same examples repo) `scripts/empty_group.py`. This script defines an empty group in its JSON output, as follows.\r\n\r\n```\r\n$ ./scripts/empty_group.py \r\n{\"ungrouped\": {\"hosts\": [\"old_host\"]}, \"ghost\": {\"hosts\": [], \"vars\": {\"foobar\": \"hello_world\"}}, \"_meta\": {\"hostvars\": {\"old_host\": {}}}}\r\n```\r\n\r\nNote that group `ghost` is included as a top-level group, with no hosts in it.\r\n\r\n```\r\n$ ansible-inventory -i scripts/empty_group.py --list\r\n{\r\n    \"_meta\": {\r\n        \"hostvars\": {\r\n            \"old_host\": {}\r\n        }\r\n    }, \r\n    \"all\": {\r\n        \"children\": [\r\n            \"ghost\", \r\n            \"ungrouped\"\r\n        ]\r\n    }, \r\n    \"ghost\": {}, \r\n    \"ungrouped\": {\r\n        \"hosts\": [\r\n            \"old_host\"\r\n        ]\r\n    }\r\n}\r\n```\r\n\r\n(related: tracking variables within this output is requested in separate issue https://github.com/ansible/ansible/issues/30877)\r\n\r\nNote that the group `ghost` is included in the output. My expectation is that the inventory example that gets `ghost` from the vars plugin would have this same kind of output.\r\n\r\nAlso, if I use [this playbook](https://github.com/AlanCoding/Ansible-inventory-file-examples/blob/master/debugging/add_host_and_echo.yml), both inventory examples will apply the group vars to the host after the host is added.\r\n\r\n```yaml\r\n---\r\n- name: Debugging playbook\r\n  hosts: all\r\n  gather_facts: false\r\n  tasks:\r\n\r\n    - name: print out names of all groups before host add\r\n      debug: msg=\"{{ groups }}\"\r\n      run_once: True\r\n\r\n    - name: add a host to group barfoo\r\n      add_host:\r\n        name: new_host\r\n        groups: ghost\r\n\r\n    - name: print out names of all groups\r\n      debug: msg=\"{{ groups }}\"\r\n      run_once: True\r\n\r\n    - name: print host variables\r\n      debug: msg=\"{{ hostvars['new_host'] }}\"\r\n      run_once: True\r\n```\r\n\r\nThe task \"print host variables\" shows that the host has the variable `\"foobar\": \"hello_world\",`, which was defined inside the file `ghost` inside of `group_vars/` folder.\r\n\r\nThis means that if you take the steps of\r\n\r\n```\r\n$ ansible-inventory -i scripts/var_files/just_host.py --list --yaml > saved.yaml\r\n$ ansible-playbook -i saved.yaml debugging/add_host_and_echo.yml \r\n```\r\n\r\nYou find different behavior. The variable `foobar` is not present in the host. This breaks the consistency between `ansible-playbook` and `ansible-inventory`.\r\n\r\n##### ACTUAL RESULTS\r\nRunning `ansible-inventory`, we find that the group named `ghost` is not present in the output where the group comes from the vars plugin.\r\n\r\n```\r\n$ ansible-inventory -i scripts/var_files/just_host.py --list\r\n{\r\n    \"_meta\": {\r\n        \"hostvars\": {\r\n            \"old_host\": {}\r\n        }\r\n    }, \r\n    \"all\": {\r\n        \"children\": [\r\n            \"ungrouped\"\r\n        ]\r\n    }, \r\n    \"ungrouped\": {\r\n        \"hosts\": [\r\n            \"old_host\"\r\n        ]\r\n    }\r\n}\r\n```\r\n\r\nThe group `ghost` is not present."},"comment":{"url":"https://api.github.com/repos/ansible/ansible/issues/comments/366988636","html_url":"https://github.com/ansible/ansible/issues/36177#issuecomment-366988636","issue_url":"https://api.github.com/repos/ansible/ansible/issues/36177","id":366988636,"user":{"login":"AlanCoding","id":1385596,"avatar_url":"https://avatars3.githubusercontent.com/u/1385596?v=4","gravatar_id":"","url":"https://api.github.com/users/AlanCoding","html_url":"https://github.com/AlanCoding","followers_url":"https://api.github.com/users/AlanCoding/followers","following_url":"https://api.github.com/users/AlanCoding/following{/other_user}","gists_url":"https://api.github.com/users/AlanCoding/gists{/gist_id}","starred_url":"https://api.github.com/users/AlanCoding/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlanCoding/subscriptions","organizations_url":"https://api.github.com/users/AlanCoding/orgs","repos_url":"https://api.github.com/users/AlanCoding/repos","events_url":"https://api.github.com/users/AlanCoding/events{/privacy}","received_events_url":"https://api.github.com/users/AlanCoding/received_events","type":"User","site_admin":false},"created_at":"2018-02-20T14:10:38Z","updated_at":"2018-02-20T14:10:38Z","author_association":"MEMBER","body":"This is more of a feature request than a bug report, because to fix the original reported AWX issue we would need some breadcrumbs from ansible to know what vars managers remain that might have yet-unseen content that can affect later tasks.\r\n\r\nBut so far, the people who reported that original bug don't seem very interested in the empty group behavior. I will close this and wait for future complaints along these lines."}}
{"actor":{"display_login":"AlanCoding"}}