Trouble with regex_replace

17 views
Skip to first unread message

Yuval Mund

unread,
Jan 26, 2017, 4:13:53 PM1/26/17
to Ansible Project
Hello,

I'm having trouble parsing a string.

The situation:
- parameters:

full: ['hostname1:abcdefg1','hostname2:-vip2','hostname3:abc-vip3']

It is required of me to have a parameter/list of hostnames without the rest of the string
i.e 
just_hostnames: ['hostname1','hostname2','hostname3']

I tried using using regex_replace like this
just_hostnames: "{{ full | regex_replace(':(?(?!'))*', '') }}"

But i got the error -
template error while templating string expected token 'end of print statement', got '.'.

If you need further information about the usecase please ask!

Thank you very much!!

Kai Stian Olstad

unread,
Jan 27, 2017, 3:31:10 AM1/27/17
to ansible...@googlegroups.com
On 26.01.2017 15:51, Yuval Mund wrote:
> I'm having trouble parsing a string.
>
> The situation:
> - parameters:
>
> full: ['hostname1:abcdefg1','hostname2:-vip2','hostname3:abc-vip3']
>
> It is required of me to have a parameter/list of hostnames without the
> rest
> of the string
> i.e
> just_hostnames: ['hostname1','hostname2','hostname3']
>
> I tried using using regex_replace like this
> just_hostnames: "{{ full | regex_replace(':(?(?!'))*', '') }}"
>
> But i got the error -
> template error while templating string expected token 'end of print
> statement', got '.'.

Because full is a list you can't do it like this since regex_replace
don't work with lists.

Bu you can do something like this.

- set_fact:
loop_item: "{{ item.split(':')[0] }}"
register: loop_result
with_items: "{{ full }}"

- set_fact:
just_hostnames: "{{ loop_result.results |
map(attribute='ansible_facts.loop_item') | list }}"

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages