# How to create a dynamic group using  group\_by and register a custom variable name

**URL:** <https://forum.ansible.com/t/how-to-create-a-dynamic-group-using-group-by-and-register-a-custom-variable-name/22185>\
**Category:** Ansible Project\
**Created:** [August 1, 2016, 6:52pm UTC](https://forum.ansible.com/t/how-to-create-a-dynamic-group-using-group-by-and-register-a-custom-variable-name/22185 "2016-08-01T18:52:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![GANA\_SAGAR](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@GANA\_SAGAR](https://forum.ansible.com/u/GANA_SAGAR)\
**Post date:** [August 1, 2016, 6:52pm UTC](https://forum.ansible.com/t/how-to-create-a-dynamic-group-using-group-by-and-register-a-custom-variable-name/22185/1 "2016-08-01T18:52:04Z")

</div>

> 0down vote[favorite](http://stackoverflow.com/questions/38571141/dynamic-groups-based-on-ip-range-in-ansible-groups#)  
>   
>   
> |   
>   
>   
> How to create variable group based on ip address-range in ansible inventory groups ? I have two groups of servers in different location. I want to create the two groups based on their IP range (two ranges for two locations) . I already have groups based on distribution, prod, dev, test, qa ,dr in my host inventory. I know I can create a thr grouping using group\_by. But How do I call them ? and How to assign a custom variable name to them ?  
>   
>   
>   
> `<br>- hosts: all<br> tasks:<br> - group_by: key=network_{{ ansible_default_ipv4.network }}<br>`  
>   
>   
> |
> 
> - | - |

But how do I call them ? and How to assign a custom variable name to them ? Is there a way that I  
could create a group once and store all the hosts in a static inventory file. ?
