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

**URL:** <https://forum.ansible.com/t/how-to-use-host-inventory-file-as-a-variable-in-playbook/16235>\
**Category:** Ansible Project\
**Created:** [September 15, 2014, 10:33pm UTC](https://forum.ansible.com/t/how-to-use-host-inventory-file-as-a-variable-in-playbook/16235 "2014-09-15T22:33:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin\_Leung](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@Benjamin\_Leung](https://forum.ansible.com/u/Benjamin_Leung)\
**Post date:** [September 15, 2014, 10:33pm UTC](https://forum.ansible.com/t/how-to-use-host-inventory-file-as-a-variable-in-playbook/16235/1 "2014-09-15T22:33:24Z")

</div>

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 ; ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 15, 2014, 10:45pm UTC](https://forum.ansible.com/t/how-to-use-host-inventory-file-as-a-variable-in-playbook/16235/2 "2014-09-15T22:45:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Benjamin\_Leung](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@Benjamin\_Leung](https://forum.ansible.com/u/Benjamin_Leung)\
**Post date:** [September 15, 2014, 11:21pm UTC](https://forum.ansible.com/t/how-to-use-host-inventory-file-as-a-variable-in-playbook/16235/3 "2014-09-15T23:21:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 16, 2014, 12:49pm UTC](https://forum.ansible.com/t/how-to-use-host-inventory-file-as-a-variable-in-playbook/16235/4 "2014-09-16T12:49:26Z")

</div>

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.
