0
votes

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

2
Are you creating specific, named hosts, or are you creating 30 of these, 20 of those, and 42 of that over there?Jack
Not named hosts.. only several servers over thereReese

2 Answers

0
votes

I put all that info in group_vars/all, so localhost would automatically have the variables it needs. So the file group_vars/all/infrastructure.yml might look like this:

environments:
- environment: prod
  region: us-east1
  servers:
  - type: app
    count: 50
    instance_type: m4.xlarge
  - type: data
    count: 15
    instance_type: m4.xlarge
- environment: test
  region: us-west2
  servers:
  - type: app
    count: 5
    instance_type: m4.xlarge
  - type: data
    count: 3
    instance_type: m4.xlarge

Now, your ec2 call has to cycle through all that.

---
- hosts: localhost
  connection: local
  gather_facts: false
  tasks:
  - name: Provision a set of instances
    ec2:
        instance_type: "{{ item.1.instance_type }}"
        image: "{{ image }}"
        region: "{{ item.0.region }}"
        vpc_subnet_id: "{{ subnets[item.0.env] }}"
        tenancy: "{{ tenancy }}"
        group_id: "{{ group_id }}"
        key_name: "{{ key_name }}"
        wait: true
        instance_tags:
           Type: "{{ item.1.type }}"
           Env: "{{ item.0.env }}"
        count_tag:
           Type: "{{ item.1.type }}"
           Env: "{{ item.0.env }}"
        exact_count: "{{ item.1.count }}"
    with_subelements:
      - "{{ environments }}"
      - servers

There's some other stuff in there you'll need to set. I do not know your environment, but this is how I built the EC2 servers for the ACA (a.k.a., Obamacare).

0
votes

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}"