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

查看:16
本文介绍了如何获取服务器名称列表并向每个名称附加资源 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.

In this example I want: 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.

我怎样才能实现我的目标?

How can I achieve my goal?

推荐答案

解决方案

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

说明

  1. map 过滤器适用过滤器(regex_replace)到列表的各个元素;

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

regex_replace 过滤器(使用下面的正则表达式)为字符串添加prefixsuffix

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

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

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

    <小时>

    替代方案

    另一种可能的解决方案(建立在您已经知道的基础上)是使用 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天全站免登陆