# Calculating subsets of a dictionary variable for different inventories

**URL:** <https://forum.ansible.com/t/calculating-subsets-of-a-dictionary-variable-for-different-inventories/15961>\
**Category:** Ansible Project\
**Tags:** fedora\
**Created:** [August 15, 2014, 4:10pm UTC](https://forum.ansible.com/t/calculating-subsets-of-a-dictionary-variable-for-different-inventories/15961 "2014-08-15T16:10:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Murray](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@Peter\_Murray](https://forum.ansible.com/u/Peter_Murray)\
**Post date:** [August 15, 2014, 4:10pm UTC](https://forum.ansible.com/t/calculating-subsets-of-a-dictionary-variable-for-different-inventories/15961/1 "2014-08-15T16:10:41Z")

</div>

We are running a small multi-tenant service, and have a large “sites” dictionary defined in group\_vars that holds the configuration data for each tenant. For example:

&nbsp;&nbsp;sites:  
&nbsp;&nbsp;&nbsp;&nbsp;demo:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drupal:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;site\_dir: demo.example.com  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;site\_name: Demo Site  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fedora:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db\_database: d\_demo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db\_username: u\_demo  
&nbsp;&nbsp;&nbsp;&nbsp;cust1:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drupal:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;site\_dir: cust1.example.com  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;site\_name: Customer 1 Site  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fedora:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db\_database: d\_cust1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db\_username: u\_cust2  
&nbsp;&nbsp;&nbsp;&nbsp;….and so on

This dictionary is used in multiple roles and plays in the playbook. We have several environments — local development, staging, preprod, and production — and we don’t want every tenant provisioned in every environment. I think this would be best done with a variable in the environment’s inventory that listed the tenants:

&nbsp;&nbsp;# production’s inventory  
&nbsp;&nbsp;…  
&nbsp;&nbsp;[all:vars]  
&nbsp;&nbsp;installed\_sites=“demo cust1”

&nbsp;&nbsp;# vagrant’s inventory  
&nbsp;&nbsp;…  
&nbsp;&nbsp;[all:vars]  
&nbsp;&nbsp;installed\_sites=“demo”

I can’t quite work out how to make this happen, though. Ideas are:

1. Use the Jinji2 intersect operation ([http://docs.ansible.com/playbooks\_variables.html#set-theory-filters](http://docs.ansible.com/playbooks_variables.html#set-theory-filters)) to make a new “installed\_sites\_data” dictionary at the start of each play run, but that didn’t give the data deep in the “sites” dictionary…only the top level keys.

2. Use something like this with each play (pseudocode):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_dict: sites  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: installed\_sites is not defined or item.key in installed\_sites  
…but this seemed cumbersome because we would have to remember to add the “when” clause every time we used the “with\_dict” statement.

3. Write our own lookup plugin ([http://docs.ansible.com/developing\_plugins.html#lookup-plugins](http://docs.ansible.com/developing_plugins.html#lookup-plugins)) that only returns data from the sites dictionary that is appropriate for a particular inventory/environment. We don’t like this one because it feels un-Ansible-like — significant logic in the playbook would be hidden behind this plug-in.

Thoughts?

Peter
