# How to filter ipaddress and hostname and store it to different variable

**URL:** <https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629>\
**Category:** Ansible Project\
**Created:** [June 21, 2019, 2:54pm UTC](https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629 "2019-06-21T14:54:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [June 21, 2019, 2:54pm UTC](https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629/1 "2019-06-21T14:54:23Z")

</div>

I have a variable that contains short hostname, FQDN and IP addresses.  
I need to filter sort hostname/FQDNs and it should store to a variable and also IP addresses should store to an another variable(this am able to do using ipaddr)  
Please help me to filter hostnames and store to a variable.

system: \>  
1.1.1.1  
[myhost.example.com](http://myhost.example.com)  
myhost

---

<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:** [June 21, 2019, 11:02pm UTC](https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629/2 "2019-06-21T23:02:41Z")

</div>

You should be able to use a regular expression to filter hostnames

---

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [June 22, 2019, 5:18am UTC](https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629/3 "2019-06-22T05:18:59Z")

</div>

Ok Thanks.

---

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [June 22, 2019, 9:43am UTC](https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629/4 "2019-06-22T09:43:47Z")

</div>

I tried below tasks but it’s giving error on regex task.  
Can you help me to get the pattern to filter only the item starts with the alphabet.

- set\_fact:  
ip\_list: “{{ ip\_addr.split(\n) | ipaddr }}”
- debug: var=ip\_list
- set\_fact:  
host\_list: “{{ ip\_addr.split(\n) | regex\_search(‘\[1\].\*’) }}”
- debug: var=host\_list

* * *

1. a-z

---

<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:** [June 22, 2019, 10:45am UTC](https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629/5 "2019-06-22T10:45:15Z")

</div>

What is the error, and what does the 'ip\_addr' variable look like?

---

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [June 22, 2019, 8:05pm UTC](https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629/6 "2019-06-22T20:05:52Z")

</div>

My variable looks below.

ip\_addr: |  
&nbsp;&nbsp;1.12.6.9  
&nbsp;&nbsp;myhost  
&nbsp;&nbsp;Myhost.example.com

---

<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:** [June 23, 2019, 3:41am UTC](https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629/7 "2019-06-23T03:41:28Z")

</div>

And the error is…?

---

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [June 23, 2019, 6:08pm UTC](https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629/8 "2019-06-23T18:08:23Z")

</div>

The error is

TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [lab1]: FAILED! =\> {“msg”: “Unexpected templating type error occurred on ({{ ip\_addr.split(\n) | regex\_search(‘\[1\].\*’) }}): expected string or buffer”}  
to retry, use: --limit @/playbooks/var\_check.retry

* * *

1. a-z

---

<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:** [June 24, 2019, 6:43pm UTC](https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629/9 "2019-06-24T18:43:35Z")

</div>

You didn't quote the newline.  
But, you don't really need that as the default is to split by white space.

For the literal IPv4 address it's indeed easy, as you can just use the  
ipaddr filter.  
For the hostname you can use the select() test, in combination with  
some regular expression that needs to match.  
Depending on how formal/exact you want to define "a hostname", the  
regex can simple or really complex.  
A simple example:

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip\_list: "{{ ip\_addr.split() | ipaddr }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=ip\_list  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host\_list: "{{ ip\_addr.split() | select('match',  
'^([a-zA-Z]+(-[a-zA-Z]+)\*\\.)+[a-zA-Z]{2,}$') | list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=host\_list

This should give you some results.

Dick

---

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [June 25, 2019, 11:16am UTC](https://forum.ansible.com/t/how-to-filter-ipaddress-and-hostname-and-store-it-to-different-variable/30629/10 "2019-06-25T11:16:06Z")

</div>

Below regex is working perfectly for my requirement. Thanks a lot, Dick for your help.

- set\_fact:  
ip\_list: “{{ ip\_addr.split() | ipaddr }}”

- debug: var=ip\_list

- set\_fact:  
host\_list: “{{ ip\_addr.split() | select(‘match’,‘^([a-zA-Z].\*)’) | list }}”

- debug: var=host\_list
