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

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,

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

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

Such as:

some_var.split(‘:’)[0]

Super, you are the man

Thank you very much !!!

Still an issue … sorry I start with Ansible:

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

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

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

:frowning:

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

Works.

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