# Add EC2 security group - only if it not already exists

**URL:** <https://forum.ansible.com/t/add-ec2-security-group-only-if-it-not-already-exists/17261>\
**Category:** Ansible Project\
**Created:** [January 14, 2015, 8:22am UTC](https://forum.ansible.com/t/add-ec2-security-group-only-if-it-not-already-exists/17261 "2015-01-14T08:22:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan](https://avatars.discourse-cdn.com/v4/letter/t/f07891/32.png) [@Tzach\_Livyatan](https://forum.ansible.com/u/Tzach_Livyatan)\
**Post date:** [January 14, 2015, 8:22am UTC](https://forum.ansible.com/t/add-ec2-security-group-only-if-it-not-already-exists/17261/1 "2015-01-14T08:22:35Z")

</div>

newbie Ansible ec2 question:

I’m successfully using ec2\_group\_module [1] to create EC2 security group.  
However, the operation fail if the group is already there  
What is the idiomatic to conditionally skip it if the group is already there?

Thanks

[1] [http://docs.ansible.com/ec2\_group\_module.html](http://docs.ansible.com/ec2_group_module.html)

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [January 14, 2015, 7:49pm UTC](https://forum.ansible.com/t/add-ec2-security-group-only-if-it-not-already-exists/17261/2 "2015-01-14T19:49:05Z")

</div>

What is the error that you get when it fails?

---

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan](https://avatars.discourse-cdn.com/v4/letter/t/f07891/32.png) [@Tzach\_Livyatan](https://forum.ansible.com/u/Tzach_Livyatan)\
**Post date:** [January 15, 2015, 7:31am UTC](https://forum.ansible.com/t/add-ec2-security-group-only-if-it-not-already-exists/17261/3 "2015-01-15T07:31:57Z")

</div>

> What is the error that you get when it fails?

400 Bad Request

Full capture below

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [January 15, 2015, 12:54pm UTC](https://forum.ansible.com/t/add-ec2-security-group-only-if-it-not-already-exists/17261/4 "2015-01-15T12:54:45Z")

</div>

hi,

I think when I was trying the ec2\_group module for some reason it will only fail if the security group already exists but was not created by the module, if you create a new security group using the ec2\_group module it wont fail when you run the same playbook again.

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [January 15, 2015, 2:05pm UTC](https://forum.ansible.com/t/add-ec2-security-group-only-if-it-not-already-exists/17261/5 "2015-01-15T14:05:56Z")

</div>

As Nicolas mentioned, you will get an error when you are trying to create the security group through Ansible and that security group already exists and was created manually/by other tool.  
If you already created the SG with Ansible and want to update it through Ansible, the task is going to report “changed”. If you simply leave it as is, at the next run, it will report “ok” =\> idempotency

Cheers.

---

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan](https://avatars.discourse-cdn.com/v4/letter/t/f07891/32.png) [@Tzach\_Livyatan](https://forum.ansible.com/u/Tzach_Livyatan)\
**Post date:** [January 18, 2015, 2:24pm UTC](https://forum.ansible.com/t/add-ec2-security-group-only-if-it-not-already-exists/17261/6 "2015-01-18T14:24:41Z")

</div>

Thanks Nicolas, Dan  
This is it.

The group was created manually.

---

<div class="post-metadata">

**Author:** ![James\_Pancoast](https://avatars.discourse-cdn.com/v4/letter/j/b2d939/32.png) [@James\_Pancoast](https://forum.ansible.com/u/James_Pancoast)\
**Post date:** [January 18, 2015, 10:04pm UTC](https://forum.ansible.com/t/add-ec2-security-group-only-if-it-not-already-exists/17261/7 "2015-01-18T22:04:22Z")

</div>

We ran into something similar a few days ago but I haven’t had time to dig into the ec2\_group code that much to figure it out.

I’m not sure if this is helpful, but if we had ‘TCP’ in all caps in the playbook it would behave like this (run fine the first time, create the group, etc., but on second attempt it would give the same error you were seeing). If I just changed it to ‘tcp’ all lower case the playbook ran fine.
