How to add a list of strings to each element?

I have a list of strings containing IP addresses. I want to add a port number to each of them. In python, I would do it something like this:

ip_list = [(ip + ":" + port) for ip in ip_list]

... but Jinja does not support lists. At the moment, I am complicating the task of creating a new list one item at a time:

{%- set ip_list = magic() %}
{%- set new_ip_list = [] %}
{%- for ip in ip_list %}
  {%- do new_ip_list.append(ip + ":" + port) %}
{%- endfor %}

It's ugly and annoying in the middle of the template, and it seems that there really needs to be a better way to get the job done. Preferably single line.

As long as I know that this can be done using custom filters, I supply a template for software that I did not write (salt plate), so they (as far as I know) are not available to me.

+4
source share
2 answers

, !

"| format"?

{% for ip in magic() -%}
curl_host_port:
  cmd.run:
    - name: {{ "curl 'http://%s:80/'"|format(ip) }}
{% endfor -%}
+1

, . "!" . .

( vars, defaults - ) :

excluded_users: ["fred","jim","bob","arthur"]

:

Match user *,!root{% if excluded_users|length > 0 %},!{{ excluded_users|join(",!") }}{% endif %}

... , :

Match user *,!root,!fred,!jim,!bob,!arthur

, ; -)

+1

Source: https://habr.com/ru/post/1615109/


All Articles