title: how to implement a multi-tenant configuration solution in ansible
body: <p>One common pattern I've seen in many applications is <a href="https://en.wikipedia.org/wiki/Multitenancy" rel="nofollow">multitenancy</a>. Basically, I'm configuring an application that has some of its configuration values which are different based on the country. The way I've managed to do this configuration is basically using a "partners" dictionary in yaml:</p>
  
  <pre><code>---
  partners:
      us: 
          url: 'http://company.com'
      uk: 
          url: 'http://company.co.uk'
      fr: 
          url: 'https://company.fr'
      ch: 
          url: 'https://company.fr'
  </code></pre>
  
  <p>And thus, when serving customers which are identified to come from 'us' we'll use one value for url that will be different if the application identifies it as coming from 'fr'.</p>
  
  <p>Then, when configuring with a template, I do something similar to:</p>
  
  <pre><code>template:  xxxx
  with_items: partners_list
  </code></pre>
  
  <p>Then, in the template I use <code>partners[item].url</code> to access the variable. Thus, with one template, I'm configuring multiple files with different values according to the partner. Bear into account that most of the values are common though (most configurations are not in the partners dict).</p>
  
  <p>This is less than satisfactory.</p>
  
  <p>What I would like to have is a "dynamic variables" system to implement something similar to a "context". Like, something simple and portable that loads contexts into your main namespace. So something that allows me to simply reference "url" and it will automatically try to use <code>partners[item].url</code> and if it's not there, use plain <code>url</code>.</p>
  
  <p>One solution would be to use <code>partners[item].url | default(url)</code>, but this is very verbose and leads to a bit of confusion IMHO. I'd rather not add so much clutter in the templates.</p>
  
  <p>Another possibility would be something like <code>"{% set_context 'us' %}" // "{% end_context %}"</code> in jinja and then overlap the variables in the dict with first level variables, with a context. I think that would be satisfactory as well. Problem is, can I extend jinja in a way that can be put in my repo, without needing to touch everyone's ansible/jinja installations? In other words, can I choose another templating engine in ansible, so I can write an extended jinja (which is able to lookup for variables) that ansible would use?</p>