# Intersect and difference in Ansible

**URL:** <https://forum.ansible.com/t/intersect-and-difference-in-ansible/33908>\
**Category:** Ansible Project\
**Created:** [September 28, 2020, 4:57am UTC](https://forum.ansible.com/t/intersect-and-difference-in-ansible/33908 "2020-09-28T04:57:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred\_Kanyesigye](https://avatars.discourse-cdn.com/v4/letter/f/46a35a/32.png) [@Fred\_Kanyesigye](https://forum.ansible.com/u/Fred_Kanyesigye)\
**Post date:** [September 28, 2020, 4:57am UTC](https://forum.ansible.com/t/intersect-and-difference-in-ansible/33908/1 "2020-09-28T04:57:16Z")

</div>

I have been trying to create a playbook that removes the resource record sets for terminated EC2 instances.

Other tasks can debug fine, but when it comes to intersect and difference to debug/list the common contents to keep and non-common to be removed, the output still brings the entire list of resource record sets. If anyone can help, I would really appreciate it.

- name: running Instances  
debug:  
msg: “{{item.0 | intersect(item.1)}}”  
with\_items:

- “{{record\_sets |json\_query(‘ResourceRecordSets.Name’)}}”

- “{{running\_ec2 |json\_query(‘instances.tags.erp\_url’)}}”

- name: terminated Instances  
debug:  
msg: “{{item.0 | difference(item.1)}}”  
with\_items:

- “{{record\_sets |json\_query(‘ResourceRecordSets.Name’)}}”

- “{{running\_ec2 |json\_query(‘instances.tags.erp\_url’)}}”
