How to use host inventory file as a variable in playbook?

53 views
Skip to first unread message

Benjamin Leung

unread,
Sep 15, 2014, 6:33:24 PM9/15/14
to ansible...@googlegroups.com
Lets say I have a playbook with the following code

  - name: Check port
    command: nc -z -v {{asdf_servers}} 8089
    ignore_errors: true

in the group var

asdf_servers: localhost

Is there a way to use the entire host inventory file as a variable in the play book? Or can I enter multiple values for the asdf_servers variable, separated by , or ; ?

Michael DeHaan

unread,
Sep 15, 2014, 6:45:11 PM9/15/14
to ansible...@googlegroups.com
Hi Benjamin,

Can you explain what you mean by "use the entire host inventory file as a variable?"

There are things like using the host selector ("hosts: groupname") and even stuff like:

 - hosts: monitored_servers
   tasks:
      - shell: /usr/bin/foo --server={{ item }}
        delegate_to: "{{ item }}"
        with_items: groups.monitoring_servers

That natively use inventory data.

The path to the inventory file is available as a variable, though in general you should not be updating that during an ansible run -- if you have that kind of use case, dynamic inventory may be appropriate.

I guess it depends on the usage - so let us know more and we'd be glad to give more details.

Thanks!

--Michael





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, 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/c4b73482-5f5a-42fa-b503-a3b8f53b6d2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Benjamin Leung

unread,
Sep 15, 2014, 7:21:57 PM9/15/14
to ansible...@googlegroups.com
Hi Michael,

I think that would work for me, using a dynamic inventory.

I basically have my inventory file setup with 2 stanzas that are receivers and senders. I am using Ansible to log into all the senders and do a net cat to verify ACL. In my playbook I do not want to have a command for every single sender host.

Basically have the asdf_servers variable equal to my host inventory [asdf_servers] stanza.

Michael DeHaan

unread,
Sep 16, 2014, 8:49:29 AM9/16/14
to ansible...@googlegroups.com
Ok, I'm so this doesn't seem like a dynamic inventory question to me, but just a good example to use "delegate_to" combined with "with_items", as you want all servers in one group to talk to the other group.

Dynamic inventory would be orthogonal to that particular question -- still useful, but not the same thing.



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages