EC2 instance + AMI + group vars

Hello,

I have 2 different environments, staging + production. For both of them I need to create AMIs with my services.

For both, I have existing stopped EC2 instances that I need to start within the playbook, then run tasks on this machine, then stop the machine and create AMI.

My problem is that I have different instances for those two environments, which I want to specify using group-vars/staging/ and group-vars/production.

e.g.:

echo “instance_id: i-123456” > group_vars/staging/instanceid.yml

echo “instance_id: i-987654” > group_vars/production/instanceid.yml

And then run

ansible-playbook -l staging deployandcreateami.yml

The problem is, that ec2 module is “local” and thus ‘instance-id’ is not defined within this context:

- name: Start sandbox instances
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
    instance_ids:
      - '{{ instance_id }}'
    region: us-east-1
  tasks:
    - name: Start the sandbox instances
      ec2:
        instance_ids: '{{ instance_ids }}'
        region: '{{ region }}'
        state: running
        wait: True
        vpc_subnet_id: subnet-29e63245
        assign_public_ip: yes
  roles:
    - do_neat_stuff
    - do_more_neat_stuff

Any hints?

Thank you!

Michal