首页 > 解决方案 > 在 Playbook 级别的 Ansible 角色中调用 jinja2 模板

问题描述

我有一个文件夹结构

Ansible:
 - roles
   - elastic.beats
 - templates
   filebeat-inputs.yml.j2

从我的剧本中,我将其称为如下:

 - { 
     role: elastic.beats,
     beat: "filebeat", 
     beat_conf: "templates/filebeat-inputs.yml.j2" 
  }

但这似乎不起作用。注意:beat_conf: 接受“值的映射结构”,因此对 yml.j2 文件的任何调用都必须采用相同的形式。

另外,我如何为角色调用多个节拍?是这样吗?或者有更清洁的方法。

  - { 
     role: elastic.beats, 
     beat: "filebeat", 
     beat_conf: "templates/filebeat-inputs.yml.j2" 
    }
  - { 
     role: elastic.beats,
     beat: "metricbeat",
     beat_conf: "templates/metribeat-inputs.yml.j2" 
    }

谢谢

只是为了提供更多背景信息:我正在尝试使用需要强制参数 beat_conf 的 Ansible elastic.beats 角色 [https://github.com/elastic/ansible-beats]。

此参数接受以下格式(映射)的值:

hosts: localhost
roles:
  - { 
      role: elastic.beats, 
      beat: filebeat
      beat_conf:
        filebeat:
          inputs:
            - type: log
              enabled: true
              paths:
                - /var/log/*.log
            - type: log
              paths:
                - /var/log/mysql.log
              scan_frequency: 10s
            - type: log
              paths:
                - /var/log/apache.log
              scan_frequency: 5s

但是,输入可以放在单独的文件($root/templates/filebeat-inputs.yml.j2)中,如下所示:

  - type: log
    enabled: true
    paths:
      - /var/log/*.log
  - type: log
    paths:
      - /var/log/mysql.log
    scan_frequency: 10s
  - type: log
    paths:
      - /var/log/apache.log
    scan_frequency: 5s
          

[参考:https://www.elastic.co/guide/en/beats/filebeat/7.12/configuration-filebeat-options.html]

如何调用此文件 ($root/templates/filebeat-inputs.yml.j2) 以便在目标上生成的最终 filebeat.yml 文件具有以下格式:

https://github.com/elastic/beats/blob/master/filebeat/filebeat.yml

标签: ansibleelastic-beats

解决方案


推荐阅读