split FQDN

20 views
Skip to first unread message

Matti Herrmann

unread,
Oct 26, 2019, 2:18:47 AM10/26/19
to Ansible Development
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?

Tom Davison

unread,
Oct 26, 2019, 8:21:38 AM10/26/19
to Matti Herrmann, Ansible Development
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] }}"

--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/cf96882c-a294-4ef7-9c35-a27ef8202caa%40googlegroups.com.

Tom Davison

unread,
Oct 26, 2019, 8:22:13 AM10/26/19
to Matti Herrmann, Ansible Development
Matti** sorry about the miss spelling

Matti Herrmann

unread,
Oct 27, 2019, 10:21:44 AM10/27/19
to Ansible Development
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.



Am Samstag, 26. Oktober 2019 14:21:38 UTC+2 schrieb Tom Davison:
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] }}"

On Sat, Oct 26, 2019 at 1:18 AM Matti Herrmann <matti-z...@web.de> wrote:
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?

--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansibl...@googlegroups.com.

Andreas Härpfer

unread,
Oct 27, 2019, 1:25:04 PM10/27/19
to Ansible Development
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']

Matti Herrmann

unread,
Oct 27, 2019, 1:51:59 PM10/27/19
to Ansible Development
Hi Andreas,

that's exactly what I need.

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

Thanks.
Reply all
Reply to author
Forward
0 new messages