How can I take a list of server names and append a resource URI and port to each?

大憨熊 提交于 2019-11-27 22:31:30

Solution

set_fact:
  fact2: "{{ fact1 | map('regex_replace', '(.*)', 'https://\\1:8000') | join(',') }}"

Explanation

  1. map filter applies a filter (regex_replace) to individual elements of the list;

  2. regex_replace filter (with the following regular expression) adds a prefix and suffix to a string;

    current_list | map('regex_replace', '(.*)', 'prefix\\1suffix')
    
  3. join filter converts the list to comma-delimited string in the output.


Alternative

Another possible solution (builds on what you already know) would be to use Jinja2 to directly for the target string:

set_fact:
  fact2: "{{ 'https://' + fact1|join(':8000,https://') + ':8000' }}"

Also you can use ternary filter:

set_fact:
  fact2: "{% for it in fact1 %}https://{{ it }}:8000{{ loop.last | ternary('', ',') }}{% endfor %}"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!