# ec2 multiple security groups using a variable

**URL:** <https://forum.ansible.com/t/ec2-multiple-security-groups-using-a-variable/18597>\
**Category:** Ansible Project\
**Created:** [June 24, 2015, 6:47pm UTC](https://forum.ansible.com/t/ec2-multiple-security-groups-using-a-variable/18597 "2015-06-24T18:47:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@Joe1](https://forum.ansible.com/u/Joe1)\
**Post date:** [June 24, 2015, 6:47pm UTC](https://forum.ansible.com/t/ec2-multiple-security-groups-using-a-variable/18597/1 "2015-06-24T18:47:13Z")

</div>

We’ve recently had to add multiple security groups to an ec2 instance, but based on the host group, it needs to be a different set of groups.  
I see that you can do this by using an array list like so:

- name: launch

local\_action:  
module: ec2  
region: us-west-2  
keypair: xxxxx  
group: [‘mygroup1’, ‘mygroup2’, ‘mygroup3’]  
instance\_type: t1.micro  
image: ami-xxxxxx  
wait: yes  
wait\_timeout: 3500

count: 2  
instance\_tags: ‘{“Name”:“stuff”}’  
register: ec2

This will work fine when hard coded, but when i replace the list with a variable it does seem to detect the groups. What am I doing wrong?

- name: launch

local\_action:  
module: ec2  
region: us-west-2  
keypair: xxxxx  
group: “{{ my\_groups }}”

instance\_type: t1.micro  
image: ami-xxxxxx  
wait: yes  
wait\_timeout: 3500  
count: 2  
instance\_tags: ‘{“Name”:“stuff”}’  
register: ec2

and set my\_groups as:  
my\_groups: [‘mygroup1’, ‘mygroup2’, ‘mygroup3’]

My guess is it has something to do with the quotes but if i remove it, I get an YAML complaint, I’ve tried it a number of different ways without success. Any ideas?

Thanks!
