Is it possible to get facts from host which are not in the current play?

61 views
Skip to first unread message

Vidar Langseid

unread,
Feb 24, 2014, 10:27:11 AM2/24/14
to ansible...@googlegroups.com
Hi

I have a inventory ( hosts ):
[webservers]
web-1
web-2

[nfsserver]
nfs-1

I then have a nfs role ( nfs.yml) :
- hosts: nfsserver
  user: root
  any_errors_fatal: yes
  roles:
    - role: common
    - role: nfsserver


For various reasons ( like getting IP of all the webservers in order to set FW rules ) I need to access facts for web-1 and web-2, but not execute any tasks on these webservers.
Is that possible to force loading of facts for groups which are not in the current play?

The only way I can figure how to do that is a bit lame:
 - let nfs.yml also include webservers in hosts: statement ( "hosts: webservers:nfsserver" )
 - set some variable for nfs-1 ( like "nfsserver=true" in inventory file)
 - in every action in roles/nfsserver/tasks/main.yml include a "when: nfsserver is defined and nfsserver == 'true'"

If not using roles at all, this is not a problem as you can include multiple playbooks, where each playbook has it's "hosts:" definition. In order to load facts for all hosts, you therefore may just include one playbook with "hosts: webservers:nfsservers", but with no actions ( or a dummy one... )

Best regards,
Vidar

Nick Groenen

unread,
Feb 24, 2014, 10:34:53 AM2/24/14
to ansible...@googlegroups.com
On Mon, Feb 24, 2014 at 4:27 PM, Vidar Langseid
<vidar.l...@gmail.com> wrote:
> - hosts: nfsserver
> user: root
> any_errors_fatal: yes
> roles:
> - role: common
> - role: nfsserver

You can hit all hosts in a separate play in the playbook, then apply
your roles only to a select group of hosts in a later play. The facts
will be available throughout the entire playbook run and not be
limited to only the hosts you specified in the second play. So instead
of your example above, you would get:

- hosts: all
gather_facts: yes

- hosts: nfsserver
user: root
any_errors_fatal: yes
roles:
- role: common
- role: nfsserver

--
Nick Groenen | zoni | @NickGroenen
https://zoni.nl | GnuPG/GPG key ID: 0xAB5382F6

Vidar Langseid

unread,
Feb 24, 2014, 11:10:05 AM2/24/14
to ansible...@googlegroups.com
Ahh....
I was not aware of the fact that you could define 2 plays in the same
file like that....
It works like a charm.. Thanx!

Best regards,
Vidar
> --
> You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/w-M_b3diQU8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGV0WwMdWMVUzMZSEN7-JJ5Y-9EG82AxBYrGnre55BjWOsin0g%40mail.gmail.com.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages