如何在Ansible模板中从变量列表构建字符串

huangapple go评论82阅读模式
英文:

How to construct a string from a list of variable in an ansible template

问题

I'd like to automatically deploy a prometheus.yml configuration file with Ansible. I need the template to create a string of targets based on a list of kafka brokers.

The list is like so:

broker:
  - kafka1
  - kafka2
  - kafka3
# - ...

I want the configuration file to look like so :

- job_name: "kafka"
  static_configs:
    - targets: ["kafka1:9500","kafka2:9500","kafka3:9500","..."]

I'd like to avoid putting the port number in the variable if possible and I'd like the string to be created no matter the number of brokers in the list.
What's the best way to do this?

英文:

I'd like to automatically deploy a prometheus.yml configuration file with Ansible. I need the template to create a string of targets based on a list of kafka brokers.

The list is like so:

broker:
  - kafka1
  - kafka2
  - kafka3
# - ...

I want the configuration file to look like so :

- job_name: "kafka"
  static_configs:
    - targets: ["kafka1:9500","kafka2:9500","kafka3:9500","..."]

I'd like to avoid putting the port number in the variable if possible and I'd like the string to be created no matter the number of brokers in the list.
What's the best way to do this?

答案1

得分: 2

你可以使用过滤器 product 来创建一个包含多个列表的列表:

broker | product([9500]):
- - kafka1
  - 9500
- - kafka2
  - 9500
- - kafka3
  - 9500

然后使用 map 来对每个子列表应用 join 过滤器,使用冒号连接,创建包含代理端口的代理列表:

broker | product([9500]) | map('join', ':'):
- kafka1:9500
- kafka2:9500
- kafka3:9500

这是你期望的 Kafka 代理与其相应端口的列表。


因此,你的代理列表应该如下所示:

- targets: "{{ broker | product([9500]) | map('join', ':') }}"

结果为:

- targets:
  - kafka1:9500
  - kafka2:9500
  - kafka3:9500

给定任务:

- debug:
    msg:
      - targets: "{{ broker | product([9500]) | map('join', ':') }}"
  vars:
    broker:
      - kafka1
      - kafka2
      - kafka3

这将产生以下结果:

ok: [localhost] => 
  msg:
  - targets:
    - kafka1:9500
    - kafka2:9500
    - kafka3:9500
英文:

You can use the filter product in order to create yourself a list of lists:

broker | product([9500]):
- - kafka1
  - 9500
- - kafka2
  - 9500
- - kafka3
  - 9500

Then use a map to apply the join filter to each of the sub lists with a colon, creating the list of brokers with their port

broker | product([9500]) | map('join', ':'):
- kafka1:9500
- kafka2:9500
- kafka3:9500

Which is your expected list of Kafka brokers with their respective ports.


So, your list of brokers should be created with

- targets: "{{ broker | product([9500]) | map('join', ':') }}"

Resulting in

- targets:
  - kafka1:9500
  - kafka2:9500
  - kafka3:9500

Given the task:

- debug:
    msg:
      - targets: "{{ broker | product([9500]) | map('join', ':') }}"
  vars:
    broker:
      - kafka1
      - kafka2
      - kafka3

This yields:

ok: [localhost] => 
  msg:
  - targets:
    - kafka1:9500
    - kafka2:9500
    - kafka3:9500

答案2

得分: 0

感谢 β.εηοιτ.βε 的回答和一些搜索,我成功在我的模板文件中编写了以下内容:

- job_name: "kafka"
  static_configs:
    - targets: [{{ broker | product([9500]) | map('join', ':') | map('to_json') | join(',') }}]

其输出为:

- job_name: "kafka"
  static_configs:
    - targets: ["kafka1:9500","kafka2:9500","kafka3:9500"]
英文:

Thanks to β.εηοιτ.βε's answer and a bit of searching, I managed to write the following in my template file :

- job_name: "kafka"
  static_configs:
    - targets: [{{ broker | product([9500]) | map('join', ':') | map('to_json') | join(',') }}]

which output was :

- job_name: "kafka"
  static_configs:
    - targets: ["kafka1:9500","kafka2:9500","kafka3:9500"]

huangapple
  • 本文由 发表于 2023年6月27日 17:01:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定