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

285 views
Skip to first unread message

Eric Feliksik

unread,
Aug 24, 2015, 9:01:48 AM8/24/15
to Ansible Project
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']? 


Brian Coca

unread,
Aug 24, 2015, 9:25:11 AM8/24/15
to Ansible Project
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.


--
Brian Coca

Eric Feliksik

unread,
Aug 27, 2015, 10:59:14 AM8/27/15
to Ansible Project
Thanks for your response Brian. 

Op maandag 24 augustus 2015 15:25:11 UTC+2 schreef Brian Coca:
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! 

Brian Coca

unread,
Aug 27, 2015, 11:06:25 AM8/27/15
to Ansible Project
On Thu, Aug 27, 2015 at 10:59 AM, Eric Feliksik <feli...@gmail.com> wrote:

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


--
Brian Coca

Eric Feliksik

unread,
Aug 27, 2015, 11:12:17 AM8/27/15
to Ansible Project
> 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.  
Reply all
Reply to author
Forward
0 new messages