# Test that a variable is a valid IP address

**URL:** <https://forum.ansible.com/t/test-that-a-variable-is-a-valid-ip-address/22157>\
**Category:** Ansible Project\
**Created:** [July 28, 2016, 9:19pm UTC](https://forum.ansible.com/t/test-that-a-variable-is-a-valid-ip-address/22157 "2016-07-28T21:19:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Groten](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@Ryan\_Groten](https://forum.ansible.com/u/Ryan_Groten)\
**Post date:** [July 28, 2016, 9:19pm UTC](https://forum.ansible.com/t/test-that-a-variable-is-a-valid-ip-address/22157/1 "2016-07-28T21:19:29Z")

</div>

I’m trying to create a task that verifies a variable is a valid IP address. The task should pass if it is valid, and fail if it’s not.  
I feel like I’m close, but not quite there with this:

`

- name: Verify IP address  
assert: { that: “{{ requested\_ip | ipaddr }}” }  
always\_run: yes  
`

This task fails properly when the value is not an IP (ipaddr filter returns False), but if it IS an IP it tries to evaluate the ip itself and throws an error (ipaddr returns the IP itself):

TASK [Verify IP address] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “The conditional check ‘10.1.4.232 != False’ failed. The error was: error while evaluating conditional (10.1.4.232 != False): float object has no element 4”}

Any suggestions?

Thanks,  
Ryan

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 28, 2016, 9:45pm UTC](https://forum.ansible.com/t/test-that-a-variable-is-a-valid-ip-address/22157/2 "2016-07-28T21:45:03Z")

</div>

According to the documentation, "that" has the same format as "when".  
Since you shouldn't use curly brackets in "when" , you should probably not use them in "that".

Try  
assert: { that: requested\_ip | ipaddr }  
and see if that fixes it.

---

<div class="post-metadata">

**Author:** ![Ryan\_Groten](https://avatars.discourse-cdn.com/v4/letter/r/f9ae1b/32.png) [@Ryan\_Groten](https://forum.ansible.com/u/Ryan_Groten)\
**Post date:** [July 29, 2016, 3:51pm UTC](https://forum.ansible.com/t/test-that-a-variable-is-a-valid-ip-address/22157/3 "2016-07-29T15:51:14Z")

</div>

Yep that was it, thanks a lot!

---

<div class="post-metadata">

**Author:** ![sirkubax](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@sirkubax](https://forum.ansible.com/u/sirkubax)\
**Post date:** [April 19, 2018, 7:19am UTC](https://forum.ansible.com/t/test-that-a-variable-is-a-valid-ip-address/22157/4 "2018-04-19T07:19:38Z")

</div>

I have a case (IP with a space in a string)

vars:

requested\_ip: ‘1 .2.3.4’ # note a space in a string

tasks:

- name: Verify IP address  
assert: { that: “{{ requested\_ip | ipaddr }}” }  
always\_run: yes

---

<div class="post-metadata">

**Author:** ![Jakub\_x](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@Jakub\_x](https://forum.ansible.com/u/Jakub_x)\
**Post date:** [April 19, 2018, 7:27am UTC](https://forum.ansible.com/t/test-that-a-variable-is-a-valid-ip-address/22157/5 "2018-04-19T07:27:15Z")

</div>

This works, but it is not ‘all in one’

- name: Verify IP address  
assert:  
that:
- requested\_ip | ipaddr
- not ’ ’ in requested\_ip

W dniu czwartek, 19 kwietnia 2018 09:19:38 UTC+2 użytkownik sirkubax napisał:
