简体   繁体   中英

ansible jinja2 template output csv format

Trying to make a failsafe logic on output as csv as comma separated each columns by JInja2 template. 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.

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

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

{% 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 ----

    - 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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