# How to filter ans extract variable in static inventory

**URL:** <https://forum.ansible.com/t/how-to-filter-ans-extract-variable-in-static-inventory/34223>\
**Category:** Ansible Project\
**Created:** [November 23, 2020, 11:36am UTC](https://forum.ansible.com/t/how-to-filter-ans-extract-variable-in-static-inventory/34223 "2020-11-23T11:36:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![blue\_fox](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@blue\_fox](https://forum.ansible.com/u/blue_fox)\
**Post date:** [November 23, 2020, 11:36am UTC](https://forum.ansible.com/t/how-to-filter-ans-extract-variable-in-static-inventory/34223/1 "2020-11-23T11:36:48Z")

</div>

Hello,

How to filter ans extract variable in static inventory.

Example:

[webserver]

10.10.10.10

20.20.20.20

---

<div class="post-metadata">

**Author:** ![Jerin\_J](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jerin\_J](https://forum.ansible.com/u/Jerin_J)\
**Post date:** [November 23, 2020, 11:40am UTC](https://forum.ansible.com/t/how-to-filter-ans-extract-variable-in-static-inventory/34223/2 "2020-11-23T11:40:44Z")

</div>

You can simply use {{inventory\_hostname}} to get the host name of the host

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 23, 2020, 11:42am UTC](https://forum.ansible.com/t/how-to-filter-ans-extract-variable-in-static-inventory/34223/3 "2020-11-23T11:42:47Z")

</div>

> Hello,
> 
> How to filter ans extract variable in static inventory.
> 
> Example:
> 
> [webserver]
> 
> 10.10.10.10
> 
> 20.20.20.20
> 
> --------------------------
> 
> i would to extract each host alone and put him in variable for use in playbook.

First entry:

groups['webserver'][0]

Second entry:

groups['webserver'][1]

Use in a task

&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: {{ groups['webserver'][0] }}

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Jerin\_J](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jerin\_J](https://forum.ansible.com/u/Jerin_J)\
**Post date:** [November 23, 2020, 11:50am UTC](https://forum.ansible.com/t/how-to-filter-ans-extract-variable-in-static-inventory/34223/4 "2020-11-23T11:50:57Z")

</div>

{{hostvars [inventory\_hostname][“environment”]}}

---

<div class="post-metadata">

**Author:** ![Jerin\_J](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jerin\_J](https://forum.ansible.com/u/Jerin_J)\
**Post date:** [November 23, 2020, 11:54am UTC](https://forum.ansible.com/t/how-to-filter-ans-extract-variable-in-static-inventory/34223/5 "2020-11-23T11:54:20Z")

</div>

Typo  
{{hostvars [inventory\_hostname][“environment”][0]}}

---

<div class="post-metadata">

**Author:** ![blue\_fox](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@blue\_fox](https://forum.ansible.com/u/blue_fox)\
**Post date:** [November 23, 2020, 12:03pm UTC](https://forum.ansible.com/t/how-to-filter-ans-extract-variable-in-static-inventory/34223/6 "2020-11-23T12:03:23Z")

</div>

Thank you for your answer,  
exactly, il like to make a loop for the role , each time he copie each host in file

example:

turn 1 for loop: copie 10.10.10.10. in file and send it to distant server  
turn 2 for loop: copie 20.20.20.20. in file and send it to distant server

Thank you
