简体   繁体   English

ansible jinja2 模板 output csv 格式

[英]ansible jinja2 template output csv format

Trying to make a failsafe logic on output as csv as comma separated each columns by JInja2 template.尝试在 output 作为 csv 上创建故障安全逻辑,因为 JInja2 模板用逗号分隔每一列。 failsafe logic is supposed to tell me if any of items in modules or tech is missing.故障安全逻辑应该告诉我模块或技术中是否有任何项目丢失。 any help appreciated to figure out the logic of jinja2 template.任何帮助了解 jinja2 模板的逻辑。

Variable多变的

swproduct_list:
  header: Sw product,sw product module,technology

  details:
    - name: BASE PACKAGE
      Modules:
        - Polygon Manager
        - Common Manager
      tech:
        - SPRING CLOUD
        - SPRING CLOUD

    - name: DMA
      Modules:
        - KUA on demand
        - KUA parameters
      tech:
        - SPRING CLOUD
        - SPRING CLOUD

Desired Output所需 Output

Sw product,sw product module,technology
DMA,KUA on demand,SPRING CLOUD
DMA,KUA parameters,SPRING CLOUD
BASE PACKAGE,Polygon Manager,SPRING CLOUD
BASE PACKAGE,Common Manager,SPRING CLOUD

Solution- Jinja2 template解决方案- Jinja2 模板

{% for intf in swproduct_list.details -%}
{% for ll in intf.Modules -%}
{{ intf.name }},{{ ll }},{{ intf.tech[loop.index0] }}
{% endfor %}
{% endfor %}

Thanks User @Zeitounator, applied below assert method before jinja template starts and it works ----感谢用户@Zeitounator,在 jinja 模板启动之前应用了下面的断言方法,它可以工作----

    - set_fact:
        check_total: |
          {
          'modules_total': {{ (swproduct_list.details | selectattr('Modules', 'defined') | map(attribute='Modules') | flatten | list) | length }},
          'tech_total': {{ (swproduct_list.details | selectattr('tech', 'defined') | map(attribute='tech') | flatten | list) | length }},
          }


    - debug:
        var: check_total

    - assert:
        that:
        - check_total.modules_total == check_total.tech_total
        quiet: true
        fail_msg: >
          total no of modules should match with total no of tech

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM