# Append newly-created security group to existing SG list in EC2 creation?

**URL:** <https://forum.ansible.com/t/append-newly-created-security-group-to-existing-sg-list-in-ec2-creation/13460>\
**Category:** Ansible Project\
**Created:** [September 11, 2013, 11:14pm UTC](https://forum.ansible.com/t/append-newly-created-security-group-to-existing-sg-list-in-ec2-creation/13460 "2013-09-11T23:14:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Shenton](https://avatars.discourse-cdn.com/v4/letter/c/f9ae1b/32.png) [@Chris\_Shenton](https://forum.ansible.com/u/Chris_Shenton)\
**Post date:** [September 11, 2013, 11:14pm UTC](https://forum.ansible.com/t/append-newly-created-security-group-to-existing-sg-list-in-ec2-creation/13460/1 "2013-09-11T23:14:53Z")

</div>

I have a set of (VPC) security group IDs, and I want to create a new (VPC) security group, then apply the existing an newly-created group to a new EC2 instance at creation time. I can create the SG, show its ID, but I cannot determine the syntax to append it to the list of already-defined SGs.

vars:

…  
ec2\_security\_ids: [‘sg-31d7155e’, ‘sg-f49c4d9b’, ‘sg-fa9c4d95’]

tasks:

- name: Create Security Group for HQTS marker interface (no rules, just right)

local\_action:  
module: ec2\_group  
name: ‘{{hqts\_name}}’  
region: ‘{{region}}’

vpc\_id: ‘{{vpc\_id}}’  
register: hqts\_sg

- name: Show SG ID  
local\_action: command echo {{ hqts\_sg.group\_id }}

- name: Launch instance HQTS  
local\_action:  
module: ec2  
keypair: ‘{{keypair}}’

group\_id: ‘{{ ec2\_security\_ids + [hqts\_sg.group\_id] }}’  
instance\_type: m1.small  
image: ami-a25415cb  
region: ${region}  
vpc\_subnet\_id: ${vpc\_1b\_web.id}  
wait: yes

register: hqts\_ec2

The SG is created and shown, but the group\_id appears to get turned into a string instead of a list and breaks the request:

TASK: [Show SG ID] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<127.0.0.1\> REMOTE\_MODULE command echo sg-b1fb5ade  
changed: [127.0.0.1] =\> {“changed”: true, “cmd”: [“echo”, “sg-b1fb5ade”], “delta”: “0:00:00.002632”, “end”: “2013-09-11 18:57:30.033285”, “rc”: 0, “start”: “2013-09-11 18:57:30.030653”, “stderr”: “”, “stdout”: “sg-b1fb5ade”}

TASK: [Launch instance HQTS] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<127.0.0.1\> REMOTE\_MODULE ec2 group\_id=‘[’“'”‘sg-31d7155e’“'”‘, ‘"’“‘sg-f49c4d9b’”’“', '”‘“‘sg-fa9c4d95’”’“‘, u’”‘“‘sg-b1fb5ade’”’"‘]’ instance\_tags=‘{“Name”: “hqts-dev”, “site”: “hqts.hq”, “op\_env”: “dev”, “Description”: “to7sandbox”, “Owner”: “shentonfreude”}’ region=us-east-1 keypair=wpizvs-cshenton instance\_type=m1.small vpc\_subnet\_id=subnet-a7cfd8cb image=ami-a25415cb  
failed: [127.0.0.1] =\> {“failed”: true, “parsed”: false}  
[…]  
File “/Users/chris/Projects/wp/ansiprime/lib/python2.7/site-packages/boto/ec2/connection.py”, line 2393, in get\_all\_security\_groups  
[(‘item’, SecurityGroup)], verb=‘POST’)  
File “/Users/chris/Projects/wp/ansiprime/lib/python2.7/site-packages/boto/connection.py”, line 1076, in get\_list  
raise self.ResponseError(response.status, response.reason, body)  
boto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request

\<?xml version="1.0" encoding="UTF-8"?\>

`InvalidGroupId.Malformed`Invalid id: “[‘sg-31d7155e’, ‘sg-f49c4d9b’, ‘sg-fa9c4d95’, u’sg-b1fb5ade’]” (expecting “sg-…”)043eabdd-3b3e-45a3-b394-d16f914b2a7e

Is there a way to do this inline, or set some variable to the SG list with the new SG appended, then reference it so it gets treated as a list instead of a string containing a list?

Thanks.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 12, 2013, 5:42pm UTC](https://forum.ansible.com/t/append-newly-created-security-group-to-existing-sg-list-in-ec2-creation/13460/2 "2013-09-12T17:42:13Z")

</div>

The security groups module should take a comma seperated list.

---

<div class="post-metadata">

**Author:** ![Chris\_Shenton](https://avatars.discourse-cdn.com/v4/letter/c/f9ae1b/32.png) [@Chris\_Shenton](https://forum.ansible.com/u/Chris_Shenton)\
**Post date:** [September 12, 2013, 9:26pm UTC](https://forum.ansible.com/t/append-newly-created-security-group-to-existing-sg-list-in-ec2-creation/13460/3 "2013-09-12T21:26:08Z")

</div>

Perhaps I’m being dense or unclear: the ‘ec2\_group’ module part runs fine. But what I’m having trouble with is referencing it and adding it to the ‘ec2’ module’s group\_id list – here:

vars:

ec2\_security\_ids: [‘sg-31d7155e’, ‘sg-f49c4d9b’, ‘sg-fa9c4d95’]

module: ec2  
group\_id: ‘{{ ec2\_security\_ids + [hqts\_sg.group\_id] }}’

Am I being stupid about the syntax for the list variable, and how to append the new SG ID to it?

Thanks again.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 13, 2013, 12:44am UTC](https://forum.ansible.com/t/append-newly-created-security-group-to-existing-sg-list-in-ec2-creation/13460/4 "2013-09-13T00:44:20Z")

</div>

Rather than using the group\_id field, could you use the group field instead and try this?

ec2: group=“{{ “,”.join(ec2\_security\_names + [hqts\_sg.group\_name]) }}”

It appears to be a bug in the code that you can’t do something similar with the group\_id field, so if you could also open a github issue for that we’d appreciate it.

---

<div class="post-metadata">

**Author:** ![Chris\_Shenton](https://avatars.discourse-cdn.com/v4/letter/c/f9ae1b/32.png) [@Chris\_Shenton](https://forum.ansible.com/u/Chris_Shenton)\
**Post date:** [September 13, 2013, 3:51pm UTC](https://forum.ansible.com/t/append-newly-created-security-group-to-existing-sg-list-in-ec2-creation/13460/5 "2013-09-13T15:51:36Z")

</div>

> Rather than using the group\_id field, could you use the group field instead and try this?
> 
> ec2: group=“{{ “,”.join(ec2\_security\_names + [hqts\_sg.group\_name]) }}”

I’m in a VPC so don’t have access to the SG names, only their IDs. ☹

> It appears to be a bug in the code that you can’t do something similar with the group\_id field, so if you could also open a github issue for that we’d appreciate it.

Will do, thanks.  
And thanks for tracking this down!
