ansible order results in a table / transpose

huangapple go评论82阅读模式
英文:

ansible order results in a table / transpose

问题

分析:您想将大约60个证书的信息转换成表格,并包含标题'common_name','serial_number','ca','type','expiry_time',和'scope',并询问是否可能实现。

想法:您可以使用Python的数据处理库来实现这个目标,例如Pandas。首先,您需要将证书信息提取到一个数据结构中,然后将其转换成表格。

推测:以下是一种可能的Python代码示例,用于实现这个任务:

import pandas as pd

# 将证书信息保存到一个列表中
certificates = [
    {
        "common_name": "cls11113",
        "serial_number": "176F67F694B6B7ED",
        "ca": "cls11113",
        "expiry_time": "2024-07-05T23:31:34+01:00",
        "scope": "svm",
        "type": "server"
    },
    # 添加其他证书信息
]

# 将列表转换为Pandas DataFrame
df = pd.DataFrame(certificates)

# 重新排列列的顺序
df = df[['common_name', 'serial_number', 'ca', 'type', 'expiry_time', 'scope']]

# 打印表格
print(df)

您可以将上述代码与您的证书信息一起使用,以将其转换为包含所需标题的表格。

汉化后的代码:以下是上述代码的汉化版本:

import pandas as pd

# 将证书信息保存到一个列表中
certificates = [
    {
        "common_name": "cls11113",
        "serial_number": "176F67F694B6B7ED",
        "ca": "cls11113",
        "expiry_time": "2024-07-05T23:31:34+01:00",
        "scope": "svm",
        "type": "server"
    },
    # 添加其他证书信息
]

# 将列表转换为Pandas DataFrame
df = pd.DataFrame(certificates)

# 重新排列列的顺序
df = df[['common_name', 'serial_number', 'ca', 'type', 'expiry_time', 'scope']]

# 打印表格
print(df)

请使用上述代码示例,根据您的实际证书信息进行调整和扩展。

英文:

the following produces information for about 60 certificates but I need to transpose it if possible in like a table with headers 'common_name','serial_number','ca','type','expiry_time','scope' - is that possible?

---
- name: NetApp Certificate Expiry Reporting
  hosts: localhost
  gather_facts: no
  collections:
    - netapp.ontap

  vars:
    pp_ontap_clusters:
      - cls1
      - cls2
      - cls3
      - cls4
      - cls5
      - cls6
      - cls7

  tasks:
  - name: run ONTAP gather facts for certificate info with specified fields
    netapp.ontap.na_ontap_rest_info:
      hostname: "{{ item }}"
      username: "{{ lookup('env', 'ontap_admin_usr') }}"
      password: "{{ lookup('env', 'ontap_admin_pwd') }}"
      use_rest: always
      https: true
      validate_certs: false
      gather_subset:
        - security/certificates
      fields:
        - 'common_name'
        - 'serial_number'
        - 'ca'
        - 'type'
        - 'expiry_time'
        - 'scope'
      parameters:
        type: server
    loop: "{{ pp_ontap_clusters }}"

this is the output for a single certificate as an example

17:28:44     "item": "cls1",
17:28:44     "ontap_info": {
17:28:44         "security/certificates": {
17:28:44             "_links": {
17:28:44                 "self": {
17:28:44                     "href": "/api/security/certificates?max_records=1024&fields=common_name%2Cserial_number%2Cca%2Ctype%2Cexpiry_time%2Cscope&type=server"
17:28:44                 }
17:28:44             },
17:28:44             "num_records": 4,
17:28:44             "records": [
17:28:44                 {
17:28:44                     "_links": {
17:28:44                         "self": {
17:28:44                             "href": "/api/security/certificates/dce3b162-1c4c-11ee-b505-00a098f2ea72"
17:28:44                         }
17:28:44                     },
17:28:44                     "ca": "cls11113",
17:28:44                     "common_name": "cls11113",
17:28:44                     "expiry_time": "2024-07-05T23:31:34+01:00",
17:28:44                     "name": "cls11113_176F67F694B6B7ED",
17:28:44                     "scope": "svm",
17:28:44                     "serial_number": "176F67F694B6B7ED",
17:28:44                     "type": "server",
17:28:44                     "uuid": "dce3b162-1c4c-11ee-b505-00a098f2ea72"
17:28:44                 },

答案1

得分: 2

将字段放入一个列表中:

  pp_ontap_fields: [common_name, serial_number, ca, type, expiry_time, scope]

然后,给定以下用于测试的数据:

    data:
      results:
        - item: cls1
          ontap_info:
            security/certificates:
              _links:
                self:
                  href: /api/security/certificates?max_records=1024&fields=common_name%2Cserial_number  type%2Cexpiry_time%2Cscope&type=server
              num_records: 4
              records:
              - _links:
                  self:
                    href: /api/security/certificates/dce3b162-1c4c-11ee-b505-00a098f2ea72
                ca: cls11113
                common_name: cls11113
                expiry_time: '2024-07-05T23:31:34+01:00'
                name: cls11113_176F67F694B6B7ED
                scope: svm
                serial_number: 176F67F694B6B7ED
                type: server
                uuid: dce3b162-1c4c-11ee-b505-00a098f2ea72

创建一个字典:

  certs: "{{ dict(data.results|json_query(certs_query)) }}"
  certs_query: '[].[item, ontap_info."security/certificates".records]'

得到:

  certs:
    cls1:
    - _links:
        self:
          href: /api/security/certificates/dce3b162-1c4c-11ee-b505-00a098f2ea72
      ca: cls11113
      common_name: cls11113
      expiry_time: '2024-07-05T23:31:34+01:00'
      name: cls11113_176F67F694B6B7ED
      scope: svm
      serial_number: 176F67F694B6B7ED
      type: server
      uuid: dce3b162-1c4c-11ee-b505-00a098f2ea72

使用字典将所有证书写入单个文件:

    - copy:
        dest: /tmp/certs.csv
        content: |
          {{ pp_ontap_fields|join(';') }}
          {% for cluster,certs in certs.items() %}
          {% for cert in certs %}
          {{ pp_ontap_fields|map('extract', cert)|join(';') }}
          {% endfor %}
          {% endfor %}          

得到:

shell> cat /tmp/certs.csv 
common_name;serial_number;ca;type;expiry_time;scope
cls11113;176F67F694B6B7ED;cls11113;server;2024-07-05T23:31:34+01:00;svm

或者按集群将证书写入文件:

    - copy:
        dest: "/tmp/certs-{{ item.key }}.csv"
        content: |
          {{ pp_ontap_fields|join(';') }}
          {% for cert in item.value %}
          {{ pp_ontap_fields|map('extract', cert)|join(';') }}
          {% endfor %}          
      loop: "{{ certs|dict2items }}"
英文:

Put the fields into a list

  pp_ontap_fields: [common_name, serial_number, ca, type, expiry_time, scope]

Then, given the below data for testing

    data:
      results:
        - item: cls1
          ontap_info:
            security/certificates:
              _links:
                self:
                  href: /api/security/certificates?max_records=1024&fields=common_name%2Cserial_number  type%2Cexpiry_time%2Cscope&type=server
              num_records: 4
              records:
              - _links:
                  self:
                    href: /api/security/certificates/dce3b162-1c4c-11ee-b505-00a098f2ea72
                ca: cls11113
                common_name: cls11113
                expiry_time: '2024-07-05T23:31:34+01:00'
                name: cls11113_176F67F694B6B7ED
                scope: svm
                serial_number: 176F67F694B6B7ED
                type: server
                uuid: dce3b162-1c4c-11ee-b505-00a098f2ea72

Create a dictionary

  certs: "{{ dict(data.results|json_query(certs_query)) }}"
  certs_query: '[].[item, ontap_info."security/certificates".records]'

gives

  certs:
    cls1:
    - _links:
        self:
          href: /api/security/certificates/dce3b162-1c4c-11ee-b505-00a098f2ea72
      ca: cls11113
      common_name: cls11113
      expiry_time: '2024-07-05T23:31:34+01:00'
      name: cls11113_176F67F694B6B7ED
      scope: svm
      serial_number: 176F67F694B6B7ED
      type: server
      uuid: dce3b162-1c4c-11ee-b505-00a098f2ea72

Use the dictionary to write all certificates into a single file

    - copy:
        dest: /tmp/certs.csv
        content: |
          {{ pp_ontap_fields|join(';') }}
          {% for cluster,certs in certs.items() %}
          {% for cert in certs %}
          {{ pp_ontap_fields|map('extract', cert)|join(';') }}
          {% endfor %}
          {% endfor %}          

gives

shell> cat /tmp/certs.csv 
common_name;serial_number;ca;type;expiry_time;scope
cls11113;176F67F694B6B7ED;cls11113;server;2024-07-05T23:31:34+01:00;svm

, or write certificates to files by clusters

    - copy:
        dest: "/tmp/certs-{{ item.key }}.csv"
        content: |
          {{ pp_ontap_fields|join(';') }}
          {% for cert in item.value %}
          {{ pp_ontap_fields|map('extract', cert)|join(';') }}
          {% endfor %}          
      loop: "{{ certs|dict2items }}"

<hr>

<sup>

Example of a complete playbook for testing

- hosts: localhost

  vars:

    pp_ontap_fields: [common_name, serial_number, ca, type, expiry_time, scope]
    
    data:
      results:
        - item: cls1
          ontap_info:
            security/certificates:
              _links:
                self:
                  href: /api/security/certificates?max_records=1024&amp;fields=common_name%2Cserial_number  type%2Cexpiry_time%2Cscope&amp;type=server
              num_records: 4
              records:
              - _links:
                  self:
                    href: /api/security/certificates/dce3b162-1c4c-11ee-b505-00a098f2ea72
                ca: cls11113
                common_name: cls11113
                expiry_time: &#39;2024-07-05T23:31:34+01:00&#39;
                name: cls11113_176F67F694B6B7ED
                scope: svm
                serial_number: 176F67F694B6B7ED
                type: server
                uuid: dce3b162-1c4c-11ee-b505-00a098f2ea72

    certs: &quot;{{ dict(data.results|json_query(certs_query)) }}&quot;
    certs_query: &#39;[].[item, ontap_info.&quot;security/certificates&quot;.records]&#39;
                
  tasks:

    - debug:
        var: certs

    - copy:
        dest: /tmp/certs.csv
        content: |
          {{ pp_ontap_fields|join(&#39;;&#39;) }}
          {% for cluster,certs in certs.items() %}
          {% for cert in certs %}
          {{ pp_ontap_fields|map(&#39;extract&#39;, cert)|join(&#39;;&#39;) }}
          {% endfor %}
          {% endfor %}          

    - copy:
        dest: &quot;/tmp/certs-{{ item.key }}.csv&quot;
        content: |
          {{ pp_ontap_fields|join(&#39;;&#39;) }}
          {% for cert in item.value %}
          {{ pp_ontap_fields|map(&#39;extract&#39;, cert)|join(&#39;;&#39;) }}
          {% endfor %}          
      loop: &quot;{{ certs|dict2items }}&quot;

</sup>

huangapple
  • 本文由 发表于 2023年7月28日 01:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定