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.
I can get the region and the availability zone from the ohai_ec2 facts.
How do I use the fact to select from a list of variables?
I tried referencing the variable directly, and that works for when statements e.g.
when: ohai_ec2.region == "us-east-1"
I tried a lookup as in Create Variable From Ansible Facts , but have not gotten the format correct.
This works
# 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
but this doesn't
...
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]') }}"
nor
- name: Base - Set timezone to US East
timezone:
name: "{{ mytz.'[ohai_ec2.region]' }}"
I get this result
fatal: [xxxxxxxxxx-xxxx]: FAILED! => {"msg": "template error while templating string: expected name or number. String: {{ mytz.[ohai_ec2.region] }}"}
What is the correct way of using the fact and variable?