# Dynamic Group Var Name

**URL:** <https://forum.ansible.com/t/dynamic-group-var-name/21281>\
**Category:** Ansible Project\
**Created:** [April 26, 2016, 9:22pm UTC](https://forum.ansible.com/t/dynamic-group-var-name/21281 "2016-04-26T21:22:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chanaka\_Samarajeewa](https://avatars.discourse-cdn.com/v4/letter/c/4da419/32.png) [@Chanaka\_Samarajeewa](https://forum.ansible.com/u/Chanaka_Samarajeewa)\
**Post date:** [April 26, 2016, 9:22pm UTC](https://forum.ansible.com/t/dynamic-group-var-name/21281/1 "2016-04-26T21:22:28Z")

</div>

Hello,

How come I cannot do the following? I get the following error when I run the play. Is there a better way to do this? Please note the group vars file has been added to the main yml file as a reference.

- mysql\_user: login\_host={{ inventory\_hostname }} login\_port={{ database\_port }} login\_user={{ database\_admin }} login\_password={{ database\_admin\_pass }} name={{ item.value.username }} password={{ item.value.password }} priv={{ item.value.privileges }} host=“%” state=present with\_dict: “rdsusers{{ env }}”

FAILED! =\> {“failed”: true, “msg”: “ERROR! with\_dict expects a dict”}

Thanks

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 3, 2016, 5:16am UTC](https://forum.ansible.com/t/dynamic-group-var-name/21281/2 "2016-05-03T05:16:48Z")

</div>

Maybe because your rdsusers{{ env }} is a list and not a dict?

Does with\_items work in your case?

Johannes

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 3, 2016, 6:28pm UTC](https://forum.ansible.com/t/dynamic-group-var-name/21281/3 "2016-05-03T18:28:54Z")

</div>

I think you want this:

with\_dict: “{{hostvars[inventory\_hostname][‘rdsusers’ + env]}}”

---

<div class="post-metadata">

**Author:** ![Chanaka\_Samarajeewa](https://avatars.discourse-cdn.com/v4/letter/c/4da419/32.png) [@Chanaka\_Samarajeewa](https://forum.ansible.com/u/Chanaka_Samarajeewa)\
**Post date:** [December 16, 2016, 6:43pm UTC](https://forum.ansible.com/t/dynamic-group-var-name/21281/4 "2016-12-16T18:43:11Z")

</div>

Hello Brian,

Can you please help me figure out how to iterate over this registered variable? Please see my question as follows:

[https://groups.google.com/forum/#!searchin/ansible-project/iterate%7Csort:relevance/ansible-project/C9fSSQGq8pM/Pcjng6yeDAAJ](https://groups.google.com/forum/#!searchin/ansible-project/iterate%7Csort:relevance/ansible-project/C9fSSQGq8pM/Pcjng6yeDAAJ)

Thanks  
Chanaka
