# In Ansible, how do I use the ec2\_group module to query if a security group exists, and then check for the result?

**URL:** <https://forum.ansible.com/t/in-ansible-how-do-i-use-the-ec2-group-module-to-query-if-a-security-group-exists-and-then-check-for-the-result/22414>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [August 29, 2016, 5:56pm UTC](https://forum.ansible.com/t/in-ansible-how-do-i-use-the-ec2-group-module-to-query-if-a-security-group-exists-and-then-check-for-the-result/22414 "2016-08-29T17:56:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [August 29, 2016, 5:56pm UTC](https://forum.ansible.com/t/in-ansible-how-do-i-use-the-ec2-group-module-to-query-if-a-security-group-exists-and-then-check-for-the-result/22414/1 "2016-08-29T17:56:47Z")

</div>

Im trying to write a role that queries a security group if it exists, and then create if if not. So I manually created the security group first via AWS GUI.

Then I tried the following for the query.

`

- name: Query if security group exits  
ec2\_group: name=“my\_test\_sg” description=“Test SG” region=“us-east-1”  
register: sg\_query  
`

But I get the following

`  
TASK [security\_group : Query if the the security group exists] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

fatal: [10.22.9.27]: FAILED! =\> {“changed”: false, “failed”: true, “module\_stderr”: “”, “module\_stdout”: “Traceback (most recent call last):\r\n File "/tmp/ansible\_jGKApb/ansible\_module\_ec2\_group.py", line 472, in \r\n main()\r\n File "/tmp/ansible\_jGKApb/ansible\_module\_ec2\_group.py", line 271, in main\r\n for curGroup in ec2.get\_all\_security\_groups():\r\n File "/usr/lib/python2.7/site-packages/boto/ec2/connection.py", line 2984, in get\_all\_security\_groups\r\n [(‘item’, SecurityGroup)], verb=‘POST’)\r\n File "/usr/lib/python2.7/site-packages/boto/connection.py", line 1186, in get\_list\r\n raise self.ResponseError(response.status, response.reason, body)\r\nboto.exception.EC2ResponseError: EC2ResponseError: 403 Forbidden\r\n\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\r\n`UnauthorizedOperation`You are not authorized to perform this operation.5b101381-5d4f-43f0-b225-7a042e6151cf\r\n”, “msg”: “MODULE FAILURE”, “parsed”: false}  
`

It looks like it’s trying to create the security group. Any clues?

Thanks.

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [August 29, 2016, 6:37pm UTC](https://forum.ansible.com/t/in-ansible-how-do-i-use-the-ec2-group-module-to-query-if-a-security-group-exists-and-then-check-for-the-result/22414/2 "2016-08-29T18:37:47Z")

</div>

Essentially, I want to do this in Ansible

`  
aws ec2 describe-security-groups --filters Name=group-name,Values=sg\_ansible\_test --query ‘SecurityGroups[\*].{Name:GroupName}’

`
