# split FQDN

**URL:** <https://forum.ansible.com/t/split-fqdn/31548>\
**Category:** Ansible Developer\
**Created:** [October 26, 2019, 6:18am UTC](https://forum.ansible.com/t/split-fqdn/31548 "2019-10-26T06:18:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matti\_Herrmann](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Matti\_Herrmann](https://forum.ansible.com/u/Matti_Herrmann)\
**Post date:** [October 26, 2019, 6:18am UTC](https://forum.ansible.com/t/split-fqdn/31548/1 "2019-10-26T06:18:46Z")

</div>

Hello,

I wan´t to split the hostname of my inventory hostnames. The hostnames have two or three subdomains. For Exampel “host.sub1.sub2.domain”  
I need two splits. One bevor the first dot (in our example: host) and one after the first dot. (sub1.sub2.domain).  
I tried with {{ HOST.split(“.”)[0] }}, but there will be split after every dot.

Any idea?

---

<div class="post-metadata">

**Author:** ![td784](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@td784](https://forum.ansible.com/u/td784)\
**Post date:** [October 26, 2019, 12:21pm UTC](https://forum.ansible.com/t/split-fqdn/31548/2 "2019-10-26T12:21:20Z")

</div>

Hi Matt,

A solution like this should work for you:

- name: “set new hostname fact”  
set\_fact:  
my\_host: “{{ inventory\_hostname.split(‘.’)[0] }}.{{ inventory\_hostname.split(‘.’)[1] }}.{{ inventory\_hostname,split(‘.’)[-1] }}”

---

<div class="post-metadata">

**Author:** ![td784](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@td784](https://forum.ansible.com/u/td784)\
**Post date:** [October 26, 2019, 12:21pm UTC](https://forum.ansible.com/t/split-fqdn/31548/3 "2019-10-26T12:21:57Z")

</div>

Matti\*\* sorry about the miss spelling

---

<div class="post-metadata">

**Author:** ![Matti\_Herrmann](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Matti\_Herrmann](https://forum.ansible.com/u/Matti_Herrmann)\
**Post date:** [October 27, 2019, 2:21pm UTC](https://forum.ansible.com/t/split-fqdn/31548/4 "2019-10-27T14:21:43Z")

</div>

Hi Tom,

this give me the result host.sub1.hostname but not host.sub1.sub2.hostname. For Hostnames with one subdomain it works fine, but not for Domains with more subdomains.

---

<div class="post-metadata">

**Author:** ![Andreas\_Harpfer](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Andreas\_Harpfer](https://forum.ansible.com/u/Andreas_Harpfer)\
**Post date:** [October 27, 2019, 5:24pm UTC](https://forum.ansible.com/t/split-fqdn/31548/5 "2019-10-27T17:24:58Z")

</div>

Hi Matti,

I'm not exactly sure what you are trying to accomplish, but I would  
like to point out that `split()` has an optional parameter `maxsplit`  
that limits the number of splits that are actually done. Shouldn't  
that help to solve your problem?

In [11]: s='host.sub1.sub2.example.com'

In [12]: s.split('.', 2)  
Out[12]: ['host', 'sub1', 'sub2.example.com']

In [13]: s.split('.', 3)  
Out[13]: ['host', 'sub1', 'sub2', 'example.com']

---

<div class="post-metadata">

**Author:** ![Matti\_Herrmann](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Matti\_Herrmann](https://forum.ansible.com/u/Matti_Herrmann)\
**Post date:** [October 27, 2019, 5:51pm UTC](https://forum.ansible.com/t/split-fqdn/31548/6 "2019-10-27T17:51:58Z")

</div>

Hi Andreas,

that’s exactly what I need.

In [12]: s.split(‘.’, 1)  
Out[12]: [‘host’, ‘[sub1.sub2.example.com](http://sub2.example.com)’]

Thanks.
