# EC2 instance + AMI + group vars

**URL:** <https://forum.ansible.com/t/ec2-instance-ami-group-vars/20900>\
**Category:** Ansible Project\
**Created:** [March 17, 2016, 10:58am UTC](https://forum.ansible.com/t/ec2-instance-ami-group-vars/20900 "2016-03-17T10:58:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michal\_Medvecky](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@Michal\_Medvecky](https://forum.ansible.com/u/Michal_Medvecky)\
**Post date:** [March 17, 2016, 10:58am UTC](https://forum.ansible.com/t/ec2-instance-ami-group-vars/20900/1 "2016-03-17T10:58:49Z")

</div>

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:

```auto
- 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
