Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja2: format + join the items of a list

Tags:

play_hosts is a list of all machines for a play. I want to take these and use something like format() to rewrite them like rabbitmq@%s and then join them together with something like join(). So:

{{ play_hosts|format(???)|join(', ') }} 

All the examples of format use piping where the input is the format string and not a list. Is there a way to use these (or something else) to accomplish what I want? The output should looks something like:

['rabbitmq@server1', 'rabbitmq@server2', rabbitmq@server3', ...] 

The jinja2 doc describes format like this:

format(value, *args, **kwargs) 

Apply python string formatting on an object:

{{ "%s - %s"|format("Hello?", "Foo!") }} -> Hello? - Foo! 

So it gives three kinds of input but doesn't describe those inputs in the example, which shows one in the pipe and the other two passed in via args. Is there a keyword arg to specify the string that's piped? Please help, python monks!

like image 773
Cignul9 Avatar asked Feb 03 '16 17:02

Cignul9


1 Answers

In ansible you can use regex_replace filter:

{{ play_hosts | map('regex_replace', '^(.*)$', 'rabbitmq@\\1') | list }} 
like image 124
user459118 Avatar answered Sep 27 '22 20:09

user459118