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

9,599 views
Skip to first unread message

Eric Franckx

unread,
Nov 12, 2015, 8:13:16 AM11/12/15
to Ansible Project
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,

Eric Franckx

unread,
Nov 12, 2015, 8:21:32 AM11/12/15
to Ansible Project
Is there a advance substr( var, ':', 0) ---> to get from the begin of the string the substring ?

Matt Martz

unread,
Nov 12, 2015, 9:59:04 AM11/12/15
to ansible...@googlegroups.com
There a few ways.  One way is to use .split()

Such as:

some_var.split(':')[0]

This email is sent on behalf of Northgate Information Solutions Limited and its associated companies ("Northgate") and is strictly confidential and intended solely for the addressee(s). 

If you are not the intended recipient of this email you must: (i) not disclose, copy or distribute its contents to any other person nor use its contents in any way or you may be acting unlawfully;  (ii) contact Northgate immediately on +44 (0)1442 232424 quoting the name of the sender and the addressee then delete it from your system.

Northgate has taken reasonable precautions to ensure that no viruses are contained in this email, but does not accept any responsibility once this email has been transmitted.  You should scan attachments (if any) for viruses.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/873a3971-60f7-4dde-850e-bfdef0697800%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Eric Franckx

unread,
Nov 12, 2015, 10:08:18 AM11/12/15
to Ansible Project
Super, you are the man

Thank you very much !!!!


On Thursday, 12 November 2015 14:13:16 UTC+1, Eric Franckx wrote:

Eric Franckx

unread,
Nov 12, 2015, 10:15:16 AM11/12/15
to Ansible Project
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

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'"}

:-(






On Thursday, 12 November 2015 14:13:16 UTC+1, Eric Franckx wrote:

Eric Franckx

unread,
Nov 12, 2015, 10:33:28 AM11/12/15
to Ansible Project
OK ... found the issue:
var=production.clients.default.hosts[0].split(':')

Works.

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


On Thursday, 12 November 2015 14:13:16 UTC+1, Eric Franckx wrote:
Reply all
Reply to author
Forward
0 new messages