# Complex host\_vars file and roles/with\_items workaround

**URL:** <https://forum.ansible.com/t/complex-host-vars-file-and-roles-with-items-workaround/19397>\
**Category:** Ansible Project\
**Created:** [September 30, 2015, 9:56pm UTC](https://forum.ansible.com/t/complex-host-vars-file-and-roles-with-items-workaround/19397 "2015-09-30T21:56:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael2](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@michael2](https://forum.ansible.com/u/michael2)\
**Post date:** [September 30, 2015, 9:56pm UTC](https://forum.ansible.com/t/complex-host-vars-file-and-roles-with-items-workaround/19397/1 "2015-09-30T21:56:12Z")

</div>

Hi everybody,

I’ve been on IRC with a bunch of questions related to the same problem, and haven’t found a good answer yet so I thought I’d post here. It’s hard to explain the full picture in detail on IRC.

**Background**

I am using Ansible 1.9.

I am using a role that reads a host\_vars file with a somewhat complex data structure. It looks something like this (the values are meaningless). There are only 3 tenants listed here, but the real-world counterpart would be closer to hundreds:

db:  
Tenant1:  
name: Tenant1  
key\_a: hello  
key\_b:

- { subkey\_a: 1, subkey\_b: 2, subkey\_c: 3 }

- { subkey\_a: 1, subkey\_b: 5, subkey\_c: 6 }

Tenant2:

name: Tenant2  
key\_a: world  
key\_b:

- { subkey\_a: 7, subkey\_b: 8, subkey\_c: 9 }
- { subkey\_a: 10, subkey\_b: 11, subkey\_c: 12 }

Tenant3:

name: Tenant3  
key\_a: bye  
key\_b:

- { subkey\_a: these, subkey\_b: values, subkey\_c: do }
- { subkey\_a: not, subkey\_b: matter, subkey\_c: thanks }

The old way of doing things was that the user would supply an extra variable ‘tenant\_id’ as a string (e.g. ‘Tenant1’ or ‘Tenant2’).  
The role would be invoked like:

roles:  
tenant\_role

and the role would execute tasks like:

- set\_fact:  
tenant: “{{ db[tenant\_id] }}”

- name: Tenant Setup  
tenant\_setup :  
tenant\_name: “{{ tenant.name }}”  
tenant\_value\_a: “{{ tenant.key\_a }}”  
tenant\_subvalue\_a: “{{ item.subkey\_a }}”  
tenant\_subvalue\_b: “{{ item.subkey\_b }}”  
tenant\_subvalue\_c: “{{ item.subkey\_c }}”  
with\_items: tenant.key\_b

Everything worked fine this way.

**New Requirements**

The new requirements is that this role be adapted to instead take in a list of tenants instead of a string, and that the parameters for the tasks in the roles be populated with corresponding data. Under the new requirements, a user would supply an extra variable ‘tenant\_list’ that might look like [‘Tenant1’, ‘Tenant3’] instead of a string as before.

The perfect fix would be to simply invoke the role using with\_items, like:

roles:

- { role: tenant\_role, tenant\_id: item, with\_items: tenant\_list }

However, I am on Ansbile 1.9 and 2.0 isn’t a possibility here. So the role must, within itself, deal with the list.

**Request for Help**

I understand there are many things on the surface that look like they might solve this, like with\_nested, with\_dict, or with\_subelement. But I can’t seem to work them into the form needed to solve this specific problem. If you think you have an answer to this problem, please post an example of how the ‘Tenant Setup’ task would look under these new requirements, and what would replace the set\_fact task above.

**Related Questions**

I am convinced I can work around this myself if I can do any of the following, so if you have answers on these please also let me know:

- Append an element to an existing list in a playbook
- Add a key/value pair to an existing hash in a playbook

Thanks so much for any help. I really appreciate it,  
Michael
