i want to load specific var files in ansible based on region and environment type. I have a playbook which creates several ec2 instances in at a go with following kind of configuration -
---
- hosts: local
gather_facts: true
any_errors_fatal: true
vars_files:
- group_vars/ec2.yml
roles:
- role-for-instance-1-creation
- role-for-instance-1-creation
but the problem is.. depending on the user requirement, it may create the instances in EU region once, in US region some other time. ec2.yml contains ec2 role related vars which may vary depending on the region , also depending on the enviroment, is it prod or is it testing. but i could not find a way.
I need some kind of structure.. where suppose user provided extra vars while running the playbook like --extra-vars "environment=prod location=EU"
and the playbook will create the ec2 instnaces in the specfic region reading specific ec2.yml file like ec2_prod_EU.yml
or like
ec2_testing_US.yml
or in better way that vars files will be loaded from specific directory
group_vars/prod/ec2-EU.yml
group_vars/testing/ec2-US.yml
how can i do that.. include_vars is an option but is there any better way to acheive it. Thanks in advance
I have done it in the following way- conditionally added vars file on the roles and the condition parameters i have passed while executing the playbook -
- name: dynamic ec2 yml load
include_vars:
file: group_vars/test/ec2_us_test_session_host_1.yml
when: environment_use == 'test' and location == 'us'
tags:
- ec2-creation
and while calling the playbook -
ansible-playbook my-playbook.yml --extra-vars "source_ami=${Source_AMI} Description_new_hosts=${description} environment_use=${environment_use} location=${location} availability_zone=${AZ} subnet=${subnet}"