# Need assistance in Regex

**URL:** <https://forum.ansible.com/t/need-assistance-in-regex/34862>\
**Category:** Ansible Project\
**Created:** [March 26, 2021, 10:37am UTC](https://forum.ansible.com/t/need-assistance-in-regex/34862 "2021-03-26T10:37:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Devops\_testing](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@Devops\_testing](https://forum.ansible.com/u/Devops_testing)\
**Post date:** [March 26, 2021, 10:37am UTC](https://forum.ansible.com/t/need-assistance-in-regex/34862/1 "2021-03-26T10:37:22Z")

</div>

I’m trying to add new IP address (10.97.6.12) in firewall, Before adding, I’m validating whether the new IP already exists in firewall or not.

**Task:**

- set\_fact:  
existing: “{{ result | json\_query(‘objects[\*].value’) | select(‘match’,( intip )) | list }}”

**output:**

TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*task path: /home/palo-test/json.yml:32  
ok: [192.168.0.40] =\> {

“ansible\_facts”: {  
“existing”: [  
“10.96.6.120”,  
“10.96.6.125”  
]  
},  
“changed”: false  
}

Regex in set\_fact is considering existing Ip objects 10.96.6.120 and 10.96.6.125 as 10.96.6.12. Need help to matching the exact IP.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [March 26, 2021, 12:13pm UTC](https://forum.ansible.com/t/need-assistance-in-regex/34862/2 "2021-03-26T12:13:04Z")

</div>

Without knowing what your 'result' data structure looks like, doing  
string matching with IP addressing is almost certainly going to be  
problematic.  
I assume you don't want to have problems.  
So, what does your 'result' data look like?

---

<div class="post-metadata">

**Author:** ![Devops\_testing](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@Devops\_testing](https://forum.ansible.com/u/Devops_testing)\
**Post date:** [March 26, 2021, 12:42pm UTC](https://forum.ansible.com/t/need-assistance-in-regex/34862/3 "2021-03-26T12:42:46Z")

</div>

Thank you for you response. Here is the result data.

{  
“objects”: [  
{  
“description”: null,  
“name”: “OUTSIDE”,  
“tag”: null,  
“type”: “ip-netmask”,  
“value”: “10.97.7.37/27”  
},  
{  
“description”: null,  
“name”: “INSIDE”,  
“tag”: null,  
“type”: “ip-netmask”,  
“value”: “10.97.7.133/27”  
},  
{  
“description”: null,  
“name”: “INTERNET”,  
“tag”: null,  
“type”: “ip-netmask”,  
“value”: “10.97.6.36/27”  
},  
{  
“description”: null,  
“name”: “Default”,  
“tag”: null,  
“type”: “ip-netmask”,  
“value”: “0.0.0.0/0”  
},  
{  
“description”: null,  
“name”: “Dummy\_server”,  
“tag”: null,  
“type”: “ip-netmask”,  
“value”: “10.96.15.254”  
},  
{  
“description”: null,  
“name”: “VPN\_Subnet”,  
“tag”: null,  
“type”: “ip-netmask”,  
“value”: “10.16.24.0/22”  
},  
{  
“description”: null,  
“name”: “Internal”,  
“tag”: null,  
“type”: “ip-netmask”,  
“value”: “10.97.12.120”  
},  
{  
“description”: null,  
“name”: “test”,  
“tag”: null,  
“type”: “ip-netmask”,  
“value”: “10.96.6.120”  
},  
{  
“description”: null,  
“name”: “test1”,  
“tag”: null,  
“type”: “ip-netmask”,  
“value”: “10.96.6.125”  
}  
]  
}

Regards,  
Sri

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [March 26, 2021, 1:23pm UTC](https://forum.ansible.com/t/need-assistance-in-regex/34862/4 "2021-03-26T13:23:17Z")

</div>

If you want 'existing' to be a boolean, then you can simple check if  
the IP is in the list.  
Assuming 'intip' is the IP for which you want to test this:

&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;existing: "{{ intip in result|json\_query('objects.value') }}"

The 'result' structure has some subnets as well, I'm not sure if it  
would be a problem when you try to add an IP address that is inside  
one of those subnets (I guess so). For instance, 10.97.7.55 would be  
part of 10.97.7.37/27, but 'existing' would still return false.  
If you want that to be covered, you'd have to glue some ipaddr login in between:

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters\_ipaddr.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters_ipaddr.html)
