# Special variable group\_names list items out of order

**URL:** <https://forum.ansible.com/t/special-variable-group-names-list-items-out-of-order/37299>\
**Category:** Ansible Project\
**Created:** [November 3, 2022, 10:57am UTC](https://forum.ansible.com/t/special-variable-group-names-list-items-out-of-order/37299 "2022-11-03T10:57:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Goran\_Kuzmanovic](https://avatars.discourse-cdn.com/v4/letter/g/8491ac/32.png) [@Goran\_Kuzmanovic](https://forum.ansible.com/u/Goran_Kuzmanovic)\
**Post date:** [November 3, 2022, 10:57am UTC](https://forum.ansible.com/t/special-variable-group-names-list-items-out-of-order/37299/1 "2022-11-03T10:57:19Z")

</div>

Hello,

While searching for my use-case, I stumbled upon this issue [https://github.com/ansible/ansible/issues/73208](https://github.com/ansible/ansible/issues/73208) when the explanation provided was “The group\_names variable is purposefully alphanumerically sorted by name, and not sorted by definition order.” However, this explanation doesn’t tell me for what purpose (in my mind, I could sort the list if I needed it that way), while I would benefit of having it sorted depending on the level. Nevertheless, is there a workaround to get the list of groups on which you can rely, level-wise ?

Thanks,  
Goran

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 3, 2022, 2:55pm UTC](https://forum.ansible.com/t/special-variable-group-names-list-items-out-of-order/37299/2 "2022-11-03T14:55:39Z")

</div>

> [https://github.com/ansible/ansible/issues/73208](https://github.com/ansible/ansible/issues/73208) when the explanation  
> provided was "The group\_names variable is purposefully alphanumerically  
> sorted by name, and not sorted by definition order." ... get the list  
> of groups on which you can rely, level-wise ?

Use \*ansible-inventory\* and parse the inventory on your own. Given  
the example from the issue

> cat hosts

all:  
&nbsp;&nbsp;children:  
&nbsp;&nbsp;&nbsp;&nbsp;NonprodAppDefault:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;children:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CHG0240322:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;controller\_env: test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hdqqclansiap001:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hdqqclbcbwtb001:

1) Declare the variables

&nbsp;&nbsp;&nbsp;&nbsp;inventory: "{{ out.stdout|from\_yaml }}"  
&nbsp;&nbsp;&nbsp;&nbsp;my\_groups: {}  
&nbsp;&nbsp;&nbsp;&nbsp;my\_groups\_0: "{{ inventory.all.children }}"

2) Parse the inventory

&nbsp;&nbsp;&nbsp;&nbsp;- command: ansible-inventory --list --yaml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: out

gives

&nbsp;&nbsp;inventory:  
&nbsp;&nbsp;&nbsp;&nbsp;all:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;children:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NonprodAppDefault:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;children:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CHG0240322:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hdqqclansiap001:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;controller\_env: test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hdqqclbcbwtb001:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;controller\_env: test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ungrouped: {}

3) Create the dictionary of your groups

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_groups: "{{ my\_groups|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine({item: \_groups}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_groups\_0.keys()|list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_groups: "{{ (my\_groups\_0[item].children|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default({})).keys()|list }}"

gives (probably what you want)

&nbsp;&nbsp;my\_groups:  
&nbsp;&nbsp;&nbsp;&nbsp;NonprodAppDefault:  
&nbsp;&nbsp;&nbsp;&nbsp;- CHG0240322  
&nbsp;&nbsp;&nbsp;&nbsp;ungrouped:
