如何获取服务器名称列表,并向每个服务器名称附加资源URI和端口? [英] How can I take a list of server names and append a resource URI and port to each?

查看:68
本文介绍了如何获取服务器名称列表,并向每个服务器名称附加资源URI和端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我合并了Ansible库存中的两个列表:

I merged two lists from an Ansible inventory:

set_fact:
  fact1: "{{ groups['group1'] + groups[group2']|list }}

输出为:

fact1:
  - server01
  - server02
  - server03

根据上述结果,我需要在前面附加 https:// ,并且在每个元素后面的端口号
然后我需要将其转换为逗号分隔列表以进行服务器配置。

With the above results, I need to append https:// to the front, and a port number to the back of each element. Then I need to convert it to a comma delimited list for a server config.

在此示例中,我要: https:// server01:8000,https:// server02:8000,https:// server03:8000

我尝试使用联接:

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

可以部分工作,但是最后一个服务器没有端口。

which partly worked but it left the last server without a port.

我如何实现我的目标?

推荐答案

解决方案

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

说明


  1. 地图过滤器将过滤器( regex_replace )应用于列表的各个元素;

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

regex_replace 过滤器(带有以下正则表达式)添加前缀后缀转换为字符串;

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

current_list | map('regex_replace', '(.*)', 'prefix\\1suffix')


  • join 过滤器会将列表转换为输出中以逗号分隔的字符串。

  • join filter converts the list to comma-delimited string in the output.






    替代

    另一个可能的解决方案(基于w您已经知道的帽子)将直接使用Jinja2作为目标字符串:

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

    这篇关于如何获取服务器名称列表,并向每个服务器名称附加资源URI和端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆