【问题标题】:How create correct template file for ansible role如何为 ansible 角色创建正确的模板文件
【发布时间】:2019-08-22 12:53:36
【问题描述】:

我需要在 yml 中创建一些具有 ansible 角色的配置文件。这个例子我想要什么:

filebeat.inputs:
- type: log
  paths:
    - /var/log/system.log
    - /var/log/wifi.log

在模板目录中的 ansible 目录结构中,我有包含此类文本的文件:

filebeat.inputs:
- type: {{filebeat_input.type}}
  {{ filebeat_input.paths | to_yaml}}

在目录默认值中,我有这样的 main.yml: filebeat_create_config: true

filebeat_input:
    type: log
    paths:
     - "/var/log/*.log"
     - "/var/somelog/*.log"

当 im run ansible-playbook with this role im 得到这个时:

filebeat.inputs:
  - type: log
  [/var/log/*.log, /var/sdfsadf]

我哪里错了?我需要改变什么和在哪里得到我想要的(见上面的例子)。 感谢您的帮助!

【问题讨论】:

    标签: ansible jinja2


    【解决方案1】:

    filebeat_input.paths 是一个列表,所以当你将它传递给to_yaml 时,你会得到一个列表。您只需要构建您的 YAML 模板,以便将列表放在正确的位置,例如:

    filebeat.inputs:
    - type: {{filebeat_input.type}}
      paths: {{ filebeat_input.paths | to_yaml}}
    

    注意:

    paths: [/var/log/*.log, /var/sdfsadf]
    

    完全等同于:

    paths:
      - /var/log/*.log
      - /var/sdfsadf
    

    【讨论】:

    • 感谢您的回答!以便一切按需要工作。再次感谢!
    猜你喜欢
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 2012-10-16
    相关资源
    最近更新 更多