limit="host_name" breaks

Given that group_name is provided, if I have the following template:

{% for host in groups[group_name] %}
{{ host }}

{{ hostvars[host] }}

{% endfor %}

And I run ansible-playbook on only a single machine of group grou_name, I will end up with just my machine gathering facts while the other machines won’t gather facts.

I understand that this might be natural to ansible (since limit says, don’t connect to other machines), but I am curious about what would be the best practice in that regard? More specifically, this apply in my case for a setup where I need to setup the first machine of a cluster application before all other machines may join in, but that machine still need access to other node configurations.

I had found myself in exactly the same situation and my approach has been the use of what I call a scope of a playbook run. Here is my main playbook:

  • name: “Gather Facts”
    hosts: “{{scope}}”
    gather_facts: true
    sudo: yes

  • name: “All the other stuff”
    hosts: “{{myhosts}}:&{{scope}}”
    gather_facts: false
    sudo: yes
    roles:

  • role1

  • role2

The first play gathers facts from everything within {{scope}} and from the second play onwards, the facts are available for everything else.

Calling the whole thing is like this:

ansible-playbook -i hosts mymain.yml --extra-vars=“myhosts=host1,host2 scope=all”

And of course you can play around with the variables. It works really nice for me but I curious to see other suggestions.

setup:
delegate_to: item
with_items: list of other hosts
register: varwithotherhostfacts

Brian’s solution is a bit much to ask of folks, and this is why we are implementing fact caching in a future release.

However, this works easily enough too:

  • hosts: all
    tasks:

  • hosts: somegroup{{additional_rules|default(‘’)}}
    tasks:

And then just pass in when you wish to use limit, “-e additional_rules=:&some_group”

This would allow passing the limit to only specific plays on using the CLI.