如何在 ansible 中循环变量或配置 ansible 清单文件的最佳方式

How to loop over a variable in ansible or best way to configure ansible inventory file

提问人:Bhanu Pratap 提问时间:10/31/2023 更新时间:10/31/2023 访问量:34

问:

有一个运行redis的主机,具有多个端口。需要通过 ansible 为特定主机上运行的每个端口配置/安装指标导出器。有关如何循环每个端口和渲染导出器配置模板的任何见解。

Example: 
host1.xyz.com
port1: 1234
port2: 2345
port3: 3456
port4: 4567

我正在尝试创建一个导出配置文件,并希望以以下格式为此主机添加配置

receivers:
  prometheus:
     config:
       scrape_configs:
         - job_name: 'redis_port1'
           scrape_interval: 30s
           metrics_path: "/metrics"
           params:
           static_configs:
             - targets: ['host1.xyz.com:1234']
           relabel_configs:
         - job_name: 'redis_port2'
           scrape_interval: 30s
           metrics_path: "/metrics"
           params:
           static_configs:
             - targets: ['host1.xyz.com:2345']
           relabel_configs:
         - job_name: 'redis_port2'
           scrape_interval: 30s
           metrics_path: "/metrics"
           params:
           static_configs:
             - targets: ['host1.xyz.com:2345']
           relabel_configs:

exporters:
  googlemanagedprometheus:

service:
  pipelines:
    metrics:
      receivers: [prometheus]
      exporters: [googlemanagedprometheus]
ansible ansible-清单 ansible-模板

评论


答: 暂无答案