程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从 anisble 中的字典中提取多个值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决从 anisble 中的字典中提取多个值?

开发过程中遇到从 anisble 中的字典中提取多个值的问题如何解决?下面主要结合日常开发的经验,给出你关于从 anisble 中的字典中提取多个值的解决方法建议,希望对你解决从 anisble 中的字典中提取多个值有所启发或帮助;

我有来自 API 链接的 JsON 数据。

---
- hosts: xxxx
  tasks:
   - name: check subs
     uri:
      url: https://xxxxx/API/v2/hosts
      user: xxxx
      password: xxxx
      method: GET
      force_basic_auth: yes
      return_content: yes
      status_code: [200]
      body_format: Json
     register: data
     ignore_errors: true

   - deBUG:
      var: data.Json['results'][0].certname
     ignore_errors: true

有了这个,我只得到第一项

ok: [xxxxx] => {
    "data.Json['results'][0].certname": "xxxxxx"

如何获取证书名称的所有值?我的密钥证书名称有 50 个值。如何在 Ansible 中执行此操作?

            "certname": “x”xxx,"comment": null,"compute_profile_id": null,"compute_profile_name": null,"compute_resource_id": null,"compute_resource_name": null,"content_faCET_attributes": {
                "applicable_module_stream_count": 0,"applicable_package_count": 28,"content_source": null,"content_source_ID": null,"content_source_name": null,"content_vIEw": {
                    "ID": 1,"name": “xxxx”
                },"content_vIEw_ID": 1,"content_vIEw_name": “xxxx”,"errata_counts": {
                    "BUGfix": 9,"enhancement": 4,"security": 4,"@R_319_10586@l": 17
                },"ID": xxx,"kickstart_repository": null,"kickstart_repository_ID": null,"kickstart_repository_name": null,"lifecycle_environment": {
                    "ID": 1,"name": "library"
                },"lifecycle_environment_ID": 1,"lifecycle_environment_name": "library","upgradable_module_stream_count": 0,"upgradable_package_count": 28,"uuID": “xxxxx”
            },"created_at": “x”xxxx,"disk": null,"domain_ID": null,"domain_name": null,"enabled": true,"environment_ID": null,"environment_name": null,"errata_status": 3,"errata_status_label": “xxx,"global_status": 2,"global_status_label": "Error","hostgroup_ID": null,"hostgroup_name": null,"hostgroup_title": null,"ID": 35,"image_file": "","image_ID": null,"image_name": null,"installed_at": null,"ip": "1x”xxxx,"iP6": null,"last_compile": "2021-06-17 08:45:41 UTC","last_report": null,"LOCATIOn_ID": 2,"LOCATIOn_name": “xx”x,"mac": “xxxx”,"managed": false,"medium_ID": null,"medium_name": null,"model_ID": 1,"model_name": “xx”x,"name": “xxx”x,"openscap_proxy": null,"openscap_proxy_ID": null,"openscap_proxy_name": null,"operaTingsystem_ID": 3,"operaTingsystem_name": "RedHat xx”,"organization_ID": x,"organization_name": “xxx”x,"owner_ID": 1,"owner_name": "Anonymous admin","owner_type": "User","provision_method": "build","ptable_ID": null,"ptable_name": null,"puppet_ca_proxy": null,"puppet_ca_proxy_ID": null,"puppet_ca_proxy_name": null,"puppet_proxy": null,"puppet_proxy_ID": null,"puppet_proxy_name": null,"puppet_status": 0,"purpose_addons_status": 3,"purpose_addons_status_label": "Not SpecifIEd","purpose_role_status": 3,"purpose_role_status_label": "Not SpecifIEd","purpose_sla_status": 3,"purpose_sla_status_label": "Not SpecifIEd","purpose_status": 3,"purpose_status_label": "Not SpecifIEd","purpose_usage_status": 3,"purpose_usage_status_label": "Not SpecifIEd","pxe_loader": null,"realm_ID": null,"realm_name": null,"sp_ip": null,"sp_mac": null,"sp_name": null,"sp_subnet_ID": null,"subnet6_ID": null,"subnet6_name": null,"subnet_ID": null,"subnet_name": null,"subscription_faCET_attributes": {
                "autoheal": true,"hypervisor": false,"ID": xx,"last_checkin": "2021-07-08 23:22:31 UTC","purpose_addons": [],"purpose_role": "","purpose_usage": "","registered_at": "2021-06-12 16:42:05 UTC","registered_through": “xxxx”,"release_version": null,"service_level": "","user": null,"subscription_global_status": 0,"subscription_status": 0,"subscription_status_label": “Unentitled”,"updated_at": "2021-06-20 00:45:49 UTC","uptime_seconds": 1882980,"use_image": null,"uuID": null
        },
   - deBUG:
      msg: "{{ data.Json.results|map(attribute='certname')|List }}"
     #when: "{{ data.Json['results']|map(attribute='subscription_status_label')|List }}" == "Unentitled"
     when: "{{ data.Json['results']|SELEctattr('subscription_status_label','search','Unentitled') }}"
     ignore_errors: true

解决方法

试试

   - debug:
      msg: "{{ data.json.results|map(attribute='certname')|list }}"

问:"仅获取那些“subscription_status”未授权的“certname”。"

A:鉴于下面的简化数据

    data:
      json:
        results:
          - certname: AAA
            comment: Company A
            subscription_status_label: Unentitled
          - certname: BBB
            comment: Company B
            subscription_status_label: Entitled
          - certname: CCC
            comment: Company C
            subscription_status_label: Unentitled

下面的任务

    - debug:
        msg: "{{ data.json.results|
                 SELEctattr('subscription_status_label','eq','Unentitled')|
                 map(attribute='certname')|list }}"

给予

  msg:
  - AAA
  - CCC

大佬总结

以上是大佬教程为你收集整理的从 anisble 中的字典中提取多个值全部内容,希望文章能够帮你解决从 anisble 中的字典中提取多个值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。