# Ansible: extrating IP address from CIDR(192.168.21.x/24)

**URL:** <https://forum.ansible.com/t/ansible-extrating-ip-address-from-cidr-192-168-21-x-24/18790>\
**Category:** Ansible Developer\
**Created:** [July 17, 2015, 3:57am UTC](https://forum.ansible.com/t/ansible-extrating-ip-address-from-cidr-192-168-21-x-24/18790 "2015-07-17T03:57:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [July 17, 2015, 3:57am UTC](https://forum.ansible.com/t/ansible-extrating-ip-address-from-cidr-192-168-21-x-24/18790/1 "2015-07-17T03:57:39Z")

</div>

I have a variable name something like this defined in json format.

“zxc\_address” : “192.168.21.x/24”

and i need to extract the IP address part(192.168.21.x) using ansible(yaml code) what is the simple solution for that? can it be done using ansible filters. if yes then how?

Thanks, VM

---

<div class="post-metadata">

**Author:** ![Henry\_Finucane](https://avatars.discourse-cdn.com/v4/letter/h/94ad74/32.png) [@Henry\_Finucane](https://forum.ansible.com/u/Henry_Finucane)\
**Post date:** [July 17, 2015, 6:16am UTC](https://forum.ansible.com/t/ansible-extrating-ip-address-from-cidr-192-168-21-x-24/18790/2 "2015-07-17T06:16:09Z")

</div>

It's not well documented but the "ipaddr" filter, in 'ip' mode, just  
returns the ip of a variable given to it:

ansible all -i localhost, -m debug -a "msg={{'192.168.21.1/24'|ipaddr('ip')}}"  
localhost | SUCCESS =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "192.168.21.1",  
&nbsp;&nbsp;&nbsp;&nbsp;"verbose\_always": true,  
&nbsp;&nbsp;&nbsp;&nbsp;"changed": false  
}

It does a lot of really useful stuff, it's worth checking the existing  
documentation: [https://docs.ansible.com/playbooks\_filters\_ipaddr.html](https://docs.ansible.com/playbooks_filters_ipaddr.html)
