Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group variables missing from hostvars #31755

Closed
joshbenner opened this issue Oct 15, 2017 · 15 comments · Fixed by #32269
Closed

Group variables missing from hostvars #31755

joshbenner opened this issue Oct 15, 2017 · 15 comments · Fixed by #32269
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. needs_info This issue requires further information. Please answer any outstanding questions. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@joshbenner
Copy link

ISSUE TYPE
  • Bug Report
COMPONENT NAME
ANSIBLE VERSION

Installed from v2.4.1.0-0.3.rc1

ansible 2.4.1.0
  config file = /Users/josh/devel/example.com/ansible/ansible.cfg
  configured module search path = [u'/Users/josh/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/josh/.virtualenvs/ansible/lib/python2.7/site-packages/ansible
  executable location = /Users/josh/.virtualenvs/ansible/bin/ansible
  python version = 2.7.10 (default, Feb  7 2017, 00:08:15) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)]
CONFIGURATION
DEFAULT_CALLBACK_WHITELIST(/Users/josh/devel/example.com/ansible/ansible.cfg) = ['foreman_custom']
DEFAULT_GATHERING(/Users/josh/devel/example.com/ansible/ansible.cfg) = smart
DEFAULT_HOST_LIST(env: ANSIBLE_INVENTORY) = [u'/Users/josh/devel/example.com/ansible/inventories/production/hosts']
DEFAULT_REMOTE_TMP(/Users/josh/devel/example.com/ansible/ansible.cfg) = /tmp/ansible
DEFAULT_REMOTE_USER(env: ANSIBLE_REMOTE_USER) = root
DEFAULT_ROLES_PATH(/Users/josh/devel/example.com/ansible/ansible.cfg) = [u'/Users/josh/devel/example.com/ansible/galaxy-roles', u'/Users/josh/devel/example.com/ansible/roles']
DEFAULT_VAULT_PASSWORD_FILE(env: ANSIBLE_VAULT_PASSWORD_FILE) = /Users/josh/.lab_vault_password
HOST_KEY_CHECKING(/Users/josh/devel/example.com/ansible/ansible.cfg) = False
RETRY_FILES_ENABLED(/Users/josh/devel/example.com/ansible/ansible.cfg) = False
OS / ENVIRONMENT

macOS sierra

SUMMARY

Variables defined in group_vars do not appear in hostvars for dynamic inventories.

STEPS TO REPRODUCE
  1. Use dynamic inventory
  2. Define some_var in group_vars/some_group
  3. Assign host to some_group
  4. Try to access hostvars['some_host']['some_var']
EXPECTED RESULTS

Variable is accessible in hostvars.

ACTUAL RESULTS

Variable is missing from hostvars.

@joshbenner
Copy link
Author

This does not happen on 2.4.0.

I run this playbook against a host in my infrastructure using 2.4.0 and 2.4.1:

- hosts: some_host
  tasks:
    - debug: msg="{{ hostvars[inventory_hostname].keys() | length }}"
    - debug: msg="{{ vars.keys() | length }}"

Output on 2.4.0:

TASK [debug] **********************************************************
ok: [some_host] => {
    "msg": "207"
}

TASK [debug] **********************************************************
ok: [some_host] => {
    "msg": "214"
}

Output on 2.4.1:

TASK [debug] **********************************************************
ok: [some_host] => {
    "msg": "130"
}

TASK [debug] **********************************************************
ok: [some_host] => {
    "msg": "214"
}

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 bug_report needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Oct 15, 2017
@alikins
Copy link
Contributor

alikins commented Oct 16, 2017

ping @bcoca

@alikins alikins added this to the 2.4.0 milestone Oct 16, 2017
@alikins
Copy link
Contributor

alikins commented Oct 16, 2017

pinging @abadger possible regression from 2.4 -> v2.4.1.0-0.3.rc1 ?

@alikins alikins removed the needs_triage Needs a first human triage before being processed. label Oct 16, 2017
@bcoca
Copy link
Member

bcoca commented Oct 16, 2017

quick test, cannot reproduce:

#>cat ~/work/inventories/group_vars/all
test1: from inv group vars

i run in both 2.4. and 2.4.1 and get same:

#>ansible -m debug -a 'var=test1' all -i ~/work/inventories/inv.py
...
"test1": "from inv group vars"
...

@joshbenner
Copy link
Author

@bcoca Could you try msg="{{ hostvars[inventory_hostname]['test1'] }}"? In my case, accessing the var directly continues to work as expected, but the variables are missing from the hostvars variable specifically.

I just confirmed the behavior using your approach with my suggested difference, which confirms what you saw, and what I'm seeing.

Following done with 2.4.1 rc3:

#>ansible some_host -m debug -a 'var=redis_version'
...
"redis_version": "4.0.2"
...

#>ansible some_host -m debug -a 'msg="{{ hostvars[inventory_hostname].redis_version }}"'
...
"failed": true,
"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'redis_version' ... "
...

@bcoca
Copy link
Member

bcoca commented Oct 16, 2017

same results

ansible -m debug -a "var=hostvars['MOCK_1478']['test1']" MOCK_1465  -i ~/work/inventories/test_inv.py
MOCK_1465 | SUCCESS => {
    "hostvars['MOCK_1478']['test1']": "from inv 'all' group vars"
}

and with your same ref:

ansible -m debug -a "var=hostvars[inventory_hostname].test1" MOCK_1465  -i ~/work/inventories/test_inv.py
MOCK_1465 | SUCCESS => {
    "hostvars[inventory_hostname].test1": "from inv 'all' group vars"
}

just to make sure, using msg:

ansible -m debug -a 'msg={{hostvars[inventory_hostname].test1}}' MOCK_1465  -i ~/work/inventories/test_inv.py
MOCK_1465 | SUCCESS => {
    "msg": "from inv 'all' group vars"
}```

@bcoca bcoca added the needs_info This issue requires further information. Please answer any outstanding questions. label Oct 16, 2017
@bcoca bcoca removed this from the 2.4.0 milestone Oct 16, 2017
@joshbenner
Copy link
Author

joshbenner commented Oct 16, 2017

When I reproduce your test more closely, I get the same result as you. The difference seems to be that the variable I'm accessing is not in the 'all' group, but in a different group the host is a member of.

inventories/production/group_vars/all/vars.yml:

platform_env: production

inventories/production/group_vars/redis-servers.yml:

redis_version: 4.0.2

I get the same results as before, with this notable additional detail:

#>ansible some_host -m debug -a "var=hostvars['some_host']['platform_env']"
...
"hostvars['some_host']['platform_env']": "production"
...

@ansibot ansibot removed the needs_info This issue requires further information. Please answer any outstanding questions. label Oct 16, 2017
@bcoca
Copy link
Member

bcoca commented Oct 16, 2017

Using another group:

#>ansible -m debug -a 'msg={{hostvars[inventory_hostname].test1}}' team  -i ~/work/inventories/test_inv.pyMOCK_1218 | SUCCESS => {
    "msg": "from team group"
}
MOCK_1221 | SUCCESS => {
    "msg": "from team group"
}
MOCK_1223 | SUCCESS => {
    "msg": "from team group"
}
MOCK_1229 | SUCCESS => {
    "msg": "from team group"
}

file used:

#>cat ~/work/inventories/group_vars/team
test1: from team group

@bcoca bcoca added the needs_info This issue requires further information. Please answer any outstanding questions. label Oct 16, 2017
@bcoca bcoca changed the title Group variables missing from hostvars when using dynamic inventory Group variables missing from hostvars Oct 16, 2017
@joshbenner
Copy link
Author

Discussed in IRC. This issue seems to only affect playbook-level variables. I've tried changing the playbook_var_root to bottom, and I still same same behavior.

Per @bcoca's request, here's a sanitized/simplified tree of what I'm working with:

.
├── action_plugins
│   └── merge_vars.py
├── ansible.cfg
├── callback_plugins
│   └── foreman_custom.py
│── debug.yml
│── filter_plugins
│   └── interfaces.py
├── group_vars
│   ├── all
│   │   ├── global_vars.yml
│   │   ├── root_keys.yml
│   │   └── vault.yml
│   ├── redis-servers.yml
│   └── sensu-servers.yml
├── inventories
│   ├── foreman.py
│   ├── production
│   │   ├── group_vars
│   │   │   ├── all
│   │   │   │   └── vars.yml
│   │   │   ├── redis-servers.yml
│   │   │   └── sensu-servers.yml
│   │   └── hosts
│   └── vagrant
│       ├── group_vars
│       │   └── all.yml
│       └── hosts
├── library
│   └── merge_vars
├── requirements.txt
├── roles
├── site.yml
└── vagrant
    ├── Vagrantfile
    └── ansible.cfg

@ansibot ansibot removed the needs_info This issue requires further information. Please answer any outstanding questions. label Oct 16, 2017
@bcoca
Copy link
Member

bcoca commented Oct 17, 2017

So I cannot reproduce with play adjacent group_vars either, simple setup:

play.yml
group_vars
 |--all
  --team

when i run:

#>ansible-playbook play.yml -i ~/work/inventories/test_inv.py
ok: [MOCK_1000] => {
    "test1": "fromteam play vars"
}

@bcoca bcoca added the needs_info This issue requires further information. Please answer any outstanding questions. label Oct 17, 2017
@victorock
Copy link
Contributor

@bcoca try specifying the inventory file in ansible.cfg.
For some reason i started to get some strange behaviour with 2.4.1 with variables missing.
In my case :

  • connection: local
  • ansible.cfg: inventory = <some_folder>
  • ansible.cfg: hash_behaviour = merge
  • play.yml residing alongside with group_vars and host_vars
  • Some hash variables defined at group_vars/all/myhash.yml and group_vars/node/myhash.yml
  • The hash content is supposed to merge.
  • issue: Hashes are missing.

Solution:

  • moved my inventory (hosts) file to the same folder as my play.yml
  • changed my ansible.cfg:inventory = hosts

Where:

  • I'am using Vagrant and i was referring in my ansible.cfg to the inventory file that is automatically generated by Vagrant.

@zigarn
Copy link
Contributor

zigarn commented Oct 27, 2017

A simple use case to reproduce the problem:
No specific ansible.cfg

# group_vars/group/all.yml 
---
key: value
# inventories/test/inventory 
[group]
localhost ansible_connection=local

Result with ansible 2.4.0.0-1

$ ansible --inventory inventories/test/inventory all -m debug -a "var=key"
localhost | SUCCESS => {
    "key": "value"
}

$ ansible --inventory inventories/test/inventory all -m debug -a "var=hostvars[inventory_hostname].key"
localhost | SUCCESS => {
    "hostvars[inventory_hostname].key": "value"
}

Result with ansible 2.4.1.0-1

$ ansible --inventory inventories/test/inventory all -m debug -a "var=key"
localhost | SUCCESS => {
    "key": "value"
}

$ ansible --inventory inventories/test/inventory all -m debug -a "var=hostvars[inventory_hostname].key"
localhost | SUCCESS => {
    "hostvars[inventory_hostname].key": "VARIABLE IS NOT DEFINED!"
}
# Same result whatever the value of ANSIBLE_PLAYBOOK_VARS_ROOT

If you move the inventory file into the root folder, or the group_vars folder into inventories/test/, the variable is resolved.
The problems seems to come from loading the group_vars from current folder different from inventory folder.

@zigarn
Copy link
Contributor

zigarn commented Oct 27, 2017

A git bisect points the commit 7f2c611 as the start for this regression.

The difference is that the basedirs variable is only filled when task is defined, but when called from hostvars.raw_get, task is undefined and so there is no basedirs and no variable is loaded.

@bcoca
Copy link
Member

bcoca commented Oct 27, 2017

see if this fixes it #32269

@zigarn
Copy link
Contributor

zigarn commented Oct 28, 2017

I confirm it fixes my use case.
@joshbenner: does it fix yours?

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. needs_info This issue requires further information. Please answer any outstanding questions. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants