# How to SUBSTRING a string from a variable like hostname\_01.com:28569 ... get only the hostname part or the port ?

**URL:** <https://forum.ansible.com/t/how-to-substring-a-string-from-a-variable-like-hostname-01-com-28569-get-only-the-hostname-part-or-the-port/19744>\
**Category:** Ansible Project\
**Created:** [November 12, 2015, 1:13pm UTC](https://forum.ansible.com/t/how-to-substring-a-string-from-a-variable-like-hostname-01-com-28569-get-only-the-hostname-part-or-the-port/19744 "2015-11-12T13:13:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Franckx](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@Eric\_Franckx](https://forum.ansible.com/u/Eric_Franckx)\
**Post date:** [November 12, 2015, 1:13pm UTC](https://forum.ansible.com/t/how-to-substring-a-string-from-a-variable-like-hostname-01-com-28569-get-only-the-hostname-part-or-the-port/19744/1 "2015-11-12T13:13:16Z")

</div>

Hi,

if I have a string/var with hostname\_01.com:28569, how can I use the hostname part (hostname\_01 and don"t use the :28569) to be use in an action ?

Regards,

---

<div class="post-metadata">

**Author:** ![Eric\_Franckx](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@Eric\_Franckx](https://forum.ansible.com/u/Eric_Franckx)\
**Post date:** [November 12, 2015, 1:21pm UTC](https://forum.ansible.com/t/how-to-substring-a-string-from-a-variable-like-hostname-01-com-28569-get-only-the-hostname-part-or-the-port/19744/2 "2015-11-12T13:21:32Z")

</div>

Is there a advance substr( var, ‘:’, 0) —\> to get from the begin of the string the substring ?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [November 12, 2015, 2:58pm UTC](https://forum.ansible.com/t/how-to-substring-a-string-from-a-variable-like-hostname-01-com-28569-get-only-the-hostname-part-or-the-port/19744/3 "2015-11-12T14:58:53Z")

</div>

There a few ways. One way is to use .split()

Such as:

some\_var.split(‘:’)[0]

---

<div class="post-metadata">

**Author:** ![Eric\_Franckx](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@Eric\_Franckx](https://forum.ansible.com/u/Eric_Franckx)\
**Post date:** [November 12, 2015, 3:08pm UTC](https://forum.ansible.com/t/how-to-substring-a-string-from-a-variable-like-hostname-01-com-28569-get-only-the-hostname-part-or-the-port/19744/4 "2015-11-12T15:08:18Z")

</div>

Super, you are the man

Thank you very much !!!

---

<div class="post-metadata">

**Author:** ![Eric\_Franckx](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@Eric\_Franckx](https://forum.ansible.com/u/Eric_Franckx)\
**Post date:** [November 12, 2015, 3:15pm UTC](https://forum.ansible.com/t/how-to-substring-a-string-from-a-variable-like-hostname-01-com-28569-get-only-the-hostname-part-or-the-port/19744/5 "2015-11-12T15:15:16Z")

</div>

Still an issue … sorry I start with Ansible:

I have a config file with following entries:  
production:  
clients:  
default:  
database: db1  
hosts:

- [host1.com:29017](http://host1.com:29017)

and so if I try on it :  
debug: msg={{ production.clients.default.hosts.split(‘:’)[0]}}

I have the following error: fatal: [[xxxxxx.com](http://xxxxxx.com)]: FAILED! =\> {“failed”: true, “msg”: “ERROR! ‘list object’ has no attribute ‘split’”}

☹

---

<div class="post-metadata">

**Author:** ![Eric\_Franckx](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@Eric\_Franckx](https://forum.ansible.com/u/Eric_Franckx)\
**Post date:** [November 12, 2015, 3:33pm UTC](https://forum.ansible.com/t/how-to-substring-a-string-from-a-variable-like-hostname-01-com-28569-get-only-the-hostname-part-or-the-port/19744/6 "2015-11-12T15:33:28Z")

</div>

OK … found the issue:  
var=production.clients.default.hosts[0].split(‘:’)

Works.

thanks again to Matt Martz for the tip some\_var.split(‘:’)[0]
