How to use '--limit A' with hostvars['B']

Assume I run a playbook with --limit A

Host A accesses hosvars[‘B’]. But these vars are not available.

I know I can write a now playbook and call it without limit, like

  • hosts: all
    gather_facts: true
  • hosts: “A”
    (( actions ))

but this requires me to write a new playbook, and defeats the purpose of the --limit option. Unfortunately host caching also doesn’t work here, as it only caches remote facts and not the vars defined in the host_vars and group_vars directories.

Another super-ugly workaround is to make the login to host B fail (set an invalid username in inventories file), but I think it’s obvious that this is terrible :wink:

So how can I use --limit in combination with hostvars[‘some_host_out_of_limit’]?

hostvars are accessible even if the host is not targeted, what is not
available are host facts (part of hostvars), in 1.8-1.9 you can use
fact caching to get around this issue and still work with --limit.
In 2.0 (current devel) you have the option of delegating fact gathering:

setup:
delegate_to: hostB

which will populate facts for hostB even if running under only hostA.

Thanks for your response Brian.

hostvars are accessible even if the host is not targeted, what is not
available are host facts (part of hostvars),

A lot of confusion now. I see the following terms of which I don’t know the exact definition:

  • facts (is this 'live facts gathered from the machine, like ansible_os_distribution)

  • inventory variables (is this “variables defined in the inventory file, and variables defined in host_vars and group_vars” ? )

  • hostvars ( is this “facts + inventory variables” ? )

In that case, I mean inventory variables here. It turns out group_vars are available, but host_vars are not. For a variable ‘fqdn’ defined in host_vars/B, it doesn’t work for me with Ansible 1.9.2.
I use {{hostvars[‘B’].fqdn}} in a playbook and run with --limit A. The error is clear:

fatal: [B] => {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘dict object’ has no attribute ‘fqdn’”, ‘failed’: True}

It does work if I do --limit A,B . Remember, the fqdn variable is defined in the yaml file host_vars/B

in 1.8-1.9 you can use
fact caching to get around this issue and still work with --limit.

I read https://github.com/ansible/ansible/issues/12048 as the exact opposite of this statement:
"Fact caching is for ‘caching facts’, group and host vars are not included, this was never intended to cache inventory variables just facts gathered from hosts to avoid the expense of gathering them every time. "

Advice is much appreciated!

A lot of confusion now. I see the following terms of which I don't know the
exact definition:
- facts (is this 'live facts gathered from the machine, like
ansible_os_distribution)
- inventory variables (is this "variables defined in the inventory file, and
variables defined in host_vars and group_vars" ? )
- hostvars ( is this "facts + inventory variables" ? )

hostvars is more than that, it has all vars available to the host at
the play execution time.

In that case, I mean inventory variables here. It turns out group_vars are
available, but host_vars are not. For a variable 'fqdn' defined in
host_vars/B, it doesn't work for me with Ansible 1.9.2.
I use {{hostvars['B'].fqdn}} in a playbook and run with --limit A. The error
is clear:

fatal: [B] => {'msg': "AnsibleUndefinedVariable: One or more undefined
variables: 'dict object' has no attribute 'fqdn'", 'failed': True}

It does work if I do --limit A,B . Remember, the fqdn variable is defined in
the yaml file host_vars/B

that seems like a bug.

I read https://github.com/ansible/ansible/issues/12048 as the exact opposite
of this statement:
"Fact caching is for 'caching facts', group and host vars are not included,
this was never intended to cache inventory variables just facts gathered
from hosts to avoid the expense of gathering them every time. "

fact caching is not applied to inventory, that does not mean inventory
won't be available.

fatal: [B] => {‘msg’: “AnsibleUndefinedVariable: One or more undefined
variables: ‘dict object’ has no attribute ‘fqdn’”, ‘failed’: True}

It does work if I do --limit A,B . Remember, the fqdn variable is defined in
the yaml file host_vars/B
that seems like a bug.

In that case, I will file a bug report on github with a minimal example to reproduce.