# Looping over nested inventory items with conditional

**URL:** <https://forum.ansible.com/t/looping-over-nested-inventory-items-with-conditional/26724>\
**Category:** Ansible Project\
**Created:** [February 13, 2018, 9:02pm UTC](https://forum.ansible.com/t/looping-over-nested-inventory-items-with-conditional/26724 "2018-02-13T21:02:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason3](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jason3](https://forum.ansible.com/u/Jason3)\
**Post date:** [February 13, 2018, 9:02pm UTC](https://forum.ansible.com/t/looping-over-nested-inventory-items-with-conditional/26724/1 "2018-02-13T21:02:57Z")

</div>

Hey all,

We’re migrating from a single load balancer pair for all systems (dev/test/qa/prod) to separate pairs for dev/test and qa/prod. As a result, my previous task to pull nodes from the pool on the load balancer will no longer suffice without some conditional logic. I have a partial solution, but it two issues.

1st Issue  
In the following code i’m attempting to make it so that if the server is in the test inventory group, it uses the dev-test f5 pair and if it’s not, use the qa-prod f5 pair. The first issue is that it loops over all combinations so the task would essentially execute twice for each f5 target.

[https://pastebin.com/ZxkPqvPF](https://pastebin.com/ZxkPqvPF)

2nd Issue  
The second problem is that i need to expand the conditional to allow multiple statements.

I need to find a way to expand this line:  
msg: “{{ (inventory\_hostname in groups[‘test’]) | ternary(hostvars[item[0]].ansible\_host,hostvars[item[1]].ansible\_host) }}”

into  
msg: “{{ (inventory\_hostname in groups[‘test’] OR inventory\_hostname in groups[‘dev’]) | ternary(hostvars[item[0]].ansible\_host,hostvars[item[1]].ansible\_host) }}”

which doesn’t work currently.

I’m wondering if there’s a way to salvage this method or if i need another approach entirely. I’ve considered just hard coding the f5’s into a group\_variable, but i’d like to avoid that and leverage the inventory instead if possible.

Thanks!

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [February 13, 2018, 10:30pm UTC](https://forum.ansible.com/t/looping-over-nested-inventory-items-with-conditional/26724/2 "2018-02-13T22:30:55Z")

</div>

I’m not entirely sure of your constraints, but it seems like you would want something more like [https://gist.github.com/flowerysong/ae18f75d2103b41198149380e8d6fb1c](https://gist.github.com/flowerysong/ae18f75d2103b41198149380e8d6fb1c) rather than a nested loop.

---

<div class="post-metadata">

**Author:** ![Jason3](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jason3](https://forum.ansible.com/u/Jason3)\
**Post date:** [February 14, 2018, 2:24pm UTC](https://forum.ansible.com/t/looping-over-nested-inventory-items-with-conditional/26724/3 "2018-02-14T14:24:01Z")

</div>

Thanks! that got me what I needed. I made this slight modification to the item piece and i’m good to go!

msg: “{{ hostvars[item].ansible\_host }}”
