title: How do I select a variable using a fact in my ansible playbook?
body: <p>I want to select different options based on the aws region and availability zone of my instances. 
   As a very simple example, I'd like to set the timezone of the instance to the correct timezone based on the region. </p>
  
  <p>I can get the region and the availability zone from the ohai_ec2 facts.</p>
  
  <p>How do I use the fact to select from a list of variables?</p>
  
  <p>I tried referencing the variable directly, and that works for when statements e.g.</p>
  
  <pre><code>when: ohai_ec2.region == "us-east-1"
  </code></pre>
  
  <p>I tried a lookup as in <a href="https://stackoverflow.com/questions/55957578/create-variable-from-ansible-facts">Create Variable From Ansible Facts</a> , but have not gotten the format correct.</p>
  
  <p>This works </p>
  
  <pre><code># find the region
            - name: Base - find the region
              debug:
                msg: "Region is {{ ohai_ec2.region }} and zone is {{ ohai_ec2.availability_zone }}"
  
  #Task: Base - Sets the timezone
            - name: Base - Set timezone to US East
              when: ohai_ec2.region == "us-east-1"
              timezone:
                name: US/Eastern
            - name: Base - Set timezone to CEST
              when: ohai_ec2.region == "eu-central-1"
              timezone:
                name: Europe/Madrid
            - name: Base - Set timezone to US West
              when: ohai_ec2.region == "us-west-2"
              timezone:
                name: US/Pacific
  </code></pre>
  
  <p>but this doesn't</p>
  
  <pre><code>...
          vars:
            mytz:
              us-east-1: "US/Eastern"
              us-west-2: "US/Pacific"
              eu-central-1: "Europe/Madrid"
  ...
  
            - name: Base - Set timezone to US East
              timezone:
                name: "{{ lookup ('vars', 'mytz'.'[ohai_ec2.region]') }}"
  </code></pre>
  
  <p>nor</p>
  
  <pre><code>          - name: Base - Set timezone to US East
              timezone:
                name: "{{ mytz.'[ohai_ec2.region]' }}"
  </code></pre>
  
  <p>I get this result </p>
  
  <pre><code>fatal: [xxxxxxxxxx-xxxx]: FAILED! =&gt; {"msg": "template error while templating string: expected name or number. String: {{ mytz.[ohai_ec2.region] }}"}
  </code></pre>
  
  <p>What is the correct way of using the fact and variable?</p>