# Nested Loops? Sorting inventory\_hostnames

**URL:** <https://forum.ansible.com/t/nested-loops-sorting-inventory-hostnames/21535>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [May 24, 2016, 9:13am UTC](https://forum.ansible.com/t/nested-loops-sorting-inventory-hostnames/21535 "2016-05-24T09:13:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Fennemore](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@Mike\_Fennemore](https://forum.ansible.com/u/Mike_Fennemore)\
**Post date:** [May 24, 2016, 9:13am UTC](https://forum.ansible.com/t/nested-loops-sorting-inventory-hostnames/21535/1 "2016-05-24T09:13:53Z")

</div>

I have a custom hostvar called server\_dependson defined in my inventory. The thinking is that each server will have a server it is dependent on.  
Therefore when running updates and rebooting (Windows servers) I want to be able to specify that certain servers be updated and rebooted based on dependency.

My initial thought was to have a nested loop, outer loop with inventory\_hostname and inner with server\_dependson. Within the loop it would compare the server\_dependson with the current inventory\_hostname from the outer loop. If it matched then it would add the inventory\_hostname to a list after the server it depended on. This way at the end of the loops I would have a sorted list of servers to feed to tasks in the list’s order.

Any ideas about how to achieve this?

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [May 24, 2016, 9:54am UTC](https://forum.ansible.com/t/nested-loops-sorting-inventory-hostnames/21535/2 "2016-05-24T09:54:13Z")

</div>

Hi Mike,

I’m curious what problem you are solving by making each server dependent on another one. Maybe worth taking a step back and asking what problem you are solving by doing this?

I’d probably look at creating some dynamic inventory if I had to do this, although I would be surprised if every server wound up being dependent on another for all hosts in an inventory. But, like I say, I don’t know what problem you are solving.

Jon

---

<div class="post-metadata">

**Author:** ![Mike\_Fennemore](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@Mike\_Fennemore](https://forum.ansible.com/u/Mike_Fennemore)\
**Post date:** [May 24, 2016, 10:50am UTC](https://forum.ansible.com/t/nested-loops-sorting-inventory-hostnames/21535/3 "2016-05-24T10:50:13Z")

</div>

Hi Jon,

We do currently use a dynamic inventory. Basically with the Windows domain servers we have instances where an application server would depend on a database server that would rely on a domain controller. In that example we would want a certain order of update and reboot.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [May 24, 2016, 12:15pm UTC](https://forum.ansible.com/t/nested-loops-sorting-inventory-hostnames/21535/4 "2016-05-24T12:15:01Z")

</div>

Thanks.

Personally I think I’d shy away from coupling this relationship in the inventory but instead use groups and enforce the order by having multiple plays in a given playbook (something like the following), but whatever works for you.

# reboot\_stuff\_playbook.yml - NOT tested, just to illustrate idea
