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

**URL:** <https://forum.ansible.com/t/how-to-use-limit-a-with-hostvars-b/19094>\
**Category:** Ansible Project\
**Created:** [August 24, 2015, 9:46am UTC](https://forum.ansible.com/t/how-to-use-limit-a-with-hostvars-b/19094 "2015-08-24T09:46:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Feliksik](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@Eric\_Feliksik](https://forum.ansible.com/u/Eric_Feliksik)\
**Post date:** [August 24, 2015, 9:46am UTC](https://forum.ansible.com/t/how-to-use-limit-a-with-hostvars-b/19094/1 "2015-08-24T09:46:49Z")

</div>

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 😉

So how can I use --limit in combination with hostvars[‘some\_host\_out\_of\_limit’]?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [August 24, 2015, 1:25pm UTC](https://forum.ansible.com/t/how-to-use-limit-a-with-hostvars-b/19094/2 "2015-08-24T13:25:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Eric\_Feliksik1](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@Eric\_Feliksik1](https://forum.ansible.com/u/Eric_Feliksik1)\
**Post date:** [August 27, 2015, 2:59pm UTC](https://forum.ansible.com/t/how-to-use-limit-a-with-hostvars-b/19094/3 "2015-08-27T14:59:14Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [August 27, 2015, 3:06pm UTC](https://forum.ansible.com/t/how-to-use-limit-a-with-hostvars-b/19094/4 "2015-08-27T15:06:16Z")

</div>

> 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](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.

---

<div class="post-metadata">

**Author:** ![Eric\_Feliksik1](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@Eric\_Feliksik1](https://forum.ansible.com/u/Eric_Feliksik1)\
**Post date:** [August 27, 2015, 3:12pm UTC](https://forum.ansible.com/t/how-to-use-limit-a-with-hostvars-b/19094/5 "2015-08-27T15:12:17Z")

</div>

> > 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.
