【问题标题】:Get the CN out of certificates information从证书信息中获取CN
【发布时间】:2022-02-14 17:23:37
【问题描述】:

我正在尝试从 Ansible 中打印出漂亮的数据,但不知道如何操作它。
这就是我在debug 中的内容——我试图以某种方式操纵数据,以便它只给我一个CN=(没有CN=)值,而不是整个主题字符串:

[
        {
            "certkey": "Verisign_ca3_cer",
            "daystoexpiration": 0,
            "status": "Expired",
            "subject": " C=US,O=VeriSign, Inc.,OU=VeriSign Trust Network,OU=Terms of use at https://www.verisign.com/rpa (c)06,CN=VeriSign Class 3 Extended Validation SSL SGC CA"
        },
        {
            "certkey": "Verisign_ca5_cer",
            "daystoexpiration": 0,
            "status": "Expired",
            "subject": " C=US,O=VeriSign, Inc.,OU=VeriSign Trust Network,OU=Terms of use at https://www.verisign.com/rpa (c)10,CN=VeriSign Class 3 International Server CA - G3"
        }
]

我想要类似这样的输出:

[
        {
            "certkey": "Verisign_ca3_cer",
            "daystoexpiration": 0,
            "status": "Expired",
            "subject": "CN=VeriSign Class 3 Extended Validation SSL SGC CA"
        },
        {
            "certkey": "Verisign_ca5_cer",
            "daystoexpiration": 0,
            "status": "Expired",
            "subject": "CN=VeriSign Class 3 International Server CA - G3"
        }
]

【问题讨论】:

    标签: filter ansible data-manipulation


    【解决方案1】:

    问:"从'subject'中选择'CN'。"

    答:假设您的数据是字典列表,例如

        certs:
          - {"certkey": "Verisign_ca3_cer",
             "daystoexpiration": 0,
             "status": "Expired",
             "subject": " C=US,O=VeriSign, ...
    

    创建已解析主题的列表,例如

        - set_fact:
            subjs: "{{ subjs|d([]) + [_subj] }}"
          loop: "{{ certs|map(attribute='subject')|list }}"
          vars:
            _subj: "{{ dict(item.split(',')|
                            select('match', '.*=.*')|
                            map('trim')|
                            map('split', '=')|
                            list) }}"
    

    给予

      subjs:
      - C: US
        CN: VeriSign Class 3 Extended Validation SSL SGC CA
        O: VeriSign
        OU: Terms of use at https://www.verisign.com/rpa (c)06
      - C: US
        CN: VeriSign Class 3 International Server CA - G3
        O: VeriSign
        OU: Terms of use at https://www.verisign.com/rpa (c)10
    

    并选择属性“CN”

        - set_fact:
            CN: "{{ subjs|map(attribute='CN')|list }}"
    

    给予

      CN:
      - VeriSign Class 3 Extended Validation SSL SGC CA
      - VeriSign Class 3 International Server CA - G3
    

    将证书与更新后的属性subject结合起来,例如

        - set_fact:
            cert_cn: "{{ cert_cn|d([]) + 
                         [item.0|combine({'subject': 'CN=' ~ item.1.CN})] }}"
          loop: "{{ certs|zip(subjs) }}"
    

    给出你想要的输出

      cert_cn:
      - certkey: Verisign_ca3_cer
        daystoexpiration: 0
        status: Expired
        subject: CN=VeriSign Class 3 Extended Validation SSL SGC CA
      - certkey: Verisign_ca5_cer
        daystoexpiration: 0
        status: Expired
        subject: CN=VeriSign Class 3 International Server CA - G3
    

    【讨论】:

    • 谢谢!我真正想要的是保持数据原样,但只有主题的 CN 部分,以及其他证书信息
    • 我明白了。是什么阻止您将其放入问题中? edit 提出问题并提出minimal reproducible example。不要忘记举一个你真正想要的例子。
    • 一个非常了不起的答案。我也想知道为什么它与问题不匹配。
    • 这只是个人观点 =) 我已经研究了半天,这对我来说很有意义,但是您的阅读方式与本意不同。
    • @Sergey Fox,请参阅更新后的答案。另见How do comments work? 和“cmets 有什么用,什么时候不应该发表评论?”尤其是。在此处删除您的 cmets。他们不提供澄清、建设性批评或次要信息。而是专注于你的问题。 SO 是一个问答网站,因此问答必须是可重复使用的。 Nobody is interested in personal views.
    【解决方案2】:

    我只是采用了 regex_replace 方式

    msg: '{{ result.json.sslcertkey | regex_replace(" C=.*?CN=", "")   }}'
    

    【讨论】:

      猜你喜欢
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 2011-03-24
      • 2016-04-05
      • 2015-04-27
      • 2011-12-13
      • 2015-09-25
      相关资源
      最近更新 更多