# Need help with a jinja2 filter

**URL:** <https://forum.ansible.com/t/need-help-with-a-jinja2-filter/23946>\
**Category:** Ansible Project\
**Created:** [March 12, 2017, 5:55pm UTC](https://forum.ansible.com/t/need-help-with-a-jinja2-filter/23946 "2017-03-12T17:55:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenton\_Brede](https://avatars.discourse-cdn.com/v4/letter/k/db5fbb/32.png) [@Kenton\_Brede](https://forum.ansible.com/u/Kenton_Brede)\
**Post date:** [March 12, 2017, 5:55pm UTC](https://forum.ansible.com/t/need-help-with-a-jinja2-filter/23946/1 "2017-03-12T17:55:09Z")

</div>

I’m trying to get the id of the security group that doesn’t have ‘Default’ in the name. In other words grab “sg-0a2fea74.”

This is what “sg\_facts | json\_query('instances[_].groups[_])” spits out:

“groups”: [  
{  
“id”: “sg-828889fe”,  
“name”: “Default / EC2 / Linux Ansible Clients”  
},  
{  
“id”: “sg-0a2fea74”,  
“name”: “EC2 / Shib Prd”  
},  
{  
“id”: “sg-eb8b8087”,  
“name”: “Default / Linux Admin Access”  
}  
],

My with\_items filter below is not working but illustrates what I’m trying to do. Anyone know how I can get this done? – Thanks

- name: Gather facts

ec2\_remote\_facts:  
region: us-east-1  
filters:  
“tag:Resource\_group\_id”: “{{ resource\_group\_id\_tag }}”  
register: sg\_facts

- debug: var={{ item }}  
with\_items: “{{ sg\_facts | json\_query(‘instances[_].groups[_].[?name !~ ‘Default’].id’) }}”

---

<div class="post-metadata">

**Author:** ![Kenton\_Brede](https://avatars.discourse-cdn.com/v4/letter/k/db5fbb/32.png) [@Kenton\_Brede](https://forum.ansible.com/u/Kenton_Brede)\
**Post date:** [March 12, 2017, 8:02pm UTC](https://forum.ansible.com/t/need-help-with-a-jinja2-filter/23946/2 "2017-03-12T20:02:57Z")

</div>

Actually “sg\_facts | json\_query('instances[_].groups[_])” spits out:

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item={u’id’: u’sg-828889fe’, u’name’: u’Default / EC2 / Linux Ansible Clients’}) =\> {  
“\<type ‘dict’\>”: “VARIABLE IS NOT DEFINED!”,  
“item”: {  
“id”: “sg-828883fe”,  
“name”: “Default / EC2 / Linux Ansible Clients”  
}  
}  
ok: [localhost] =\> (item={u’id’: u’sg-eb8b8087’, u’name’: u’Default / Linux Admin Access / Port 22’}) =\> {  
“\<type ‘dict’\>”: “VARIABLE IS NOT DEFINED!”,  
“item”: {  
“id”: “sg-eb8b8087”,  
“name”: “Default / Linux Admin Access / Port 22”  
}  
}  
ok: [localhost] =\> (item={u’id’: u’sg-0a2fea74’, u’name’: u’EC2 / ITS Shib Prd’}) =\> {  
“\<type ‘dict’\>”: “VARIABLE IS NOT DEFINED!”,  
“item”: {  
“id”: “sg-0a2fea74”,  
“name”: “EC2 / ITS Shib Prd”  
}  
}
