# Conditional loop using hostvars

**URL:** <https://forum.ansible.com/t/conditional-loop-using-hostvars/4362>\
**Category:** Get Help\
**Tags:** loop\
**Created:** [March 13, 2024, 7:24pm UTC](https://forum.ansible.com/t/conditional-loop-using-hostvars/4362 "2024-03-13T19:24:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chtsalid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@chtsalid](https://forum.ansible.com/u/chtsalid)\
**Post date:** [March 13, 2024, 7:24pm UTC](https://forum.ansible.com/t/conditional-loop-using-hostvars/4362/1 "2024-03-13T19:24:11Z")

</div>

Hi all,

I am encountering a problem using a conditional loop with hostvars. I am getting an error, that the conditional is not correct. Here underneath is my module:

set\_fact:  
hostlist: “{{ hostlist | default() + [item] }}”  
loop: “{{ groups[‘ios’] }}”  
when: hostvars.[‘item’].[‘failed\_attempt’] == “failed”

I am using ansible 2.10.

Do you have any idea, how can I fix this?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [March 13, 2024, 8:49pm UTC](https://forum.ansible.com/t/conditional-loop-using-hostvars/4362/2 "2024-03-13T20:49:45Z")

</div>

```auto
when: hostvars.['item'].['failed_attempt'] == "failed"

```

You did not include the error (which is something you should definitely do when you want help), but: this is not valid syntax. `.` is an accessor, as is `[]`; you should only use one of them for each access.

```auto
when: hostvars['item']['failed_attempt'] == "failed"

```

or

```auto
when: hostvars.item.failed_attempt == "failed"

```

_However_, what you probably actually want is

```auto
when: hostvars[item]['failed_attempt'] == "failed"

```

or

```auto
when: hostvars[item].failed_attempt == "failed"

```

with an unquoted `item` so that the value of the variable is used rather than the literal string.

---

<div class="post-metadata">

**Author:** ![chtsalid](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@chtsalid](https://forum.ansible.com/u/chtsalid)\
**Post date:** [March 14, 2024, 6:14am UTC](https://forum.ansible.com/t/conditional-loop-using-hostvars/4362/4 "2024-03-14T06:14:08Z")

</div>

Hi,

Thank you for you support. I gave it a try, but it didn’t work.

> ```
> set_fact:
> hostlist: "{{ hostlist | default([]) + [item] }}"
> loop: "{{ groups['ios'] }}"
> when: hostvars[item]['failed_attempt'] == "failed"
> 
> ```

And the error:

> fatal: [xxx]: FAILED! =\> {“msg”: “The conditional check ‘hostvars[item][‘failed\_attempt’] == "failed"’ failed. The error was: error while evaluating conditional (hostvars[item][‘failed\_attempt’] == "failed"): ‘ansible.vars.hostvars.HostVarsVars object’ has no attribute ‘failed\_attempt’\n\nThe error appears to be in ‘xxx’: line 103, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: "Create a list with all failed attempts"\n ^ here\n”}

---

<div class="post-metadata">

**Author:** ![gwmngilfen](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gwmngilfen/32/6_2.png) [@gwmngilfen](https://forum.ansible.com/u/gwmngilfen)\
**Post date:** [March 14, 2024, 9:15am UTC](https://forum.ansible.com/t/conditional-loop-using-hostvars/4362/6 "2024-03-14T09:15:57Z")

</div>

Hi @chtsalid - just note that I’ve deleted your duplicate posts. I’m assuming your were trying to edit your original post, so I’ve left the one that seems formatted best.
