# Issue when iterating over hostvars attribute

**URL:** <https://forum.ansible.com/t/issue-when-iterating-over-hostvars-attribute/38215>\
**Category:** Ansible Project\
**Created:** [July 11, 2023, 7:42pm UTC](https://forum.ansible.com/t/issue-when-iterating-over-hostvars-attribute/38215 "2023-07-11T19:42:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [July 11, 2023, 7:42pm UTC](https://forum.ansible.com/t/issue-when-iterating-over-hostvars-attribute/38215/1 "2023-07-11T19:42:30Z")

</div>

Hey folks,

Not sure if I’m too tired today or what but I’m stuck on a relatively easy task. Here’s the thing:  
I have a task which runs a command for each host in the inventory. For each host, I will need to run this command as many times as elements in “{{ hostvars[inventory\_hostname][‘hostgroups’] }}”. However I’m getting the list as literal on each loop, which is not what I want.

Here’s the task and the output:

- name: ‘Adding servers to each respective hostgroup’  
ansible.builtin.debug:  
msg: ‘echo command-hostgroup addhostgroup {{ inventory\_hostname }} {{ item }}’  
register: ‘\_\_r\_tb\_hostgroups\_add’  
changed\_when: false  
loop:
- “{{ hostvars[inventory\_hostname][‘hostgroups’] }}”

TASK [Adding servers to each respective hostgroup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [aaa → localhost] =\> (item=[‘foobar\_service\_web’, ‘foobar\_service\_unstable’]) =\> {  
**“msg”: “echo command-hostgroup addhostgroup aaa [‘foobar\_service\_web’, ‘foobar\_service\_unstable’]”**  
}  
ok: [bbb → localhost] =\> (item=[‘foobar\_service\_db’, ‘foobar\_service\_testing’]) =\> {  
**“msg”: “echo command-hostgroup addhostgroup bbb [‘foobar\_service\_db’, ‘foobar\_service\_testing’]”**  
}  
ok: [ccc → localhost] =\> (item=[‘foobar\_service\_app’, ‘foobar\_service\_stable’]) =\> {  
**“msg”: “echo command-hostgroup addhostgroup ccc [‘foobar\_service\_app’, ‘foobar\_service\_stable’]”**  
}

Any thoughts?

Thanks,

---

<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:** [July 11, 2023, 8:11pm UTC](https://forum.ansible.com/t/issue-when-iterating-over-hostvars-attribute/38215/2 "2023-07-11T20:11:53Z")

</div>

Try

&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "echo {{ inventory\_hostname }} {{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ hostgroups|from\_yaml }}"

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [July 11, 2023, 9:38pm UTC](https://forum.ansible.com/t/issue-when-iterating-over-hostvars-attribute/38215/3 "2023-07-11T21:38:13Z")

</div>

I’ve finally managed to get it working by using with\_items instead of loop  
Thanks anyway Vlad,  
Cheers
