使用ansib时如何从dict获取值返回

2024-09-30 01:34:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近打算用ansible调用vraapi。我可以使用uri模式从API获取json结果返回。但是,在将请求发布到API之前,如何获取id的“Reboot a machine.”值(“07831a32-0bc7-4f7d-8372-164ea69d61af”)。你知道吗

这里的逻辑是先循环content项,然后保存id值(“07831a32-0bc7-4f7d-8372-164ea69d61af”)在匹配关键字时注册(重新启动计算机)

但是代码并没有像我期望的那样工作。你知道吗

有人能帮我提些建议吗。如何处理这个问题。你知道吗

- name: get actions list
  uri: xxxxxxx
      ......
  register: actions


- name: var_aaa
  when: actions.json.content[{{ item }}]['description'] == "Delete a snapshot of this machine."
  with_sequence: start=0 end={{ (actions.json.content|count) -1 }}
  register: actions.json.content[{{ item }}]['id']

这里是示例json

/

    "json": {
        "content": [
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": null,
                "description": "Infrastructure connect using VMRC",
                "extensionId": "csp.places.iaas.item.nowindow.ConnectViaNativeVmrc",
                "formScale": null,
                "hasForm": false,
                "iconId": "cafe_default_icon_genericResourceOperation",
                "id": "b0f8dd39-4f69-47c9-8344-5b2fce37cab5",
                "name": "Connect using VMRC",
                "providerTypeId": null,
                "type": "EXTENSION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Virtual.Action.DeleteSnapshot",
                "description": "Delete a snapshot of this machine.",
                "extensionId": null,
                "formScale": "SMALL",
                "hasForm": true,
                "iconId": "virtualDeleteSnapshot.png",
                "id": "6493a45c-4216-4a03-a866-b1ec0636288a",
                "name": "Delete Snapshot",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Virtual.Action.Destroy",
                "description": "Destroy a virtual machine.",
                "extensionId": null,
                "formScale": null,
                "hasForm": false,
                "iconId": "virtualDestroy.png",
                "id": "6f3f00cc-96e7-4c18-92a6-cdf75236b58a",
                "name": "Destroy",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Machine.Action.Expire",
                "description": "Expire a machine.",
                "extensionId": null,
                "formScale": null,
                "hasForm": false,
                "iconId": "machineExpire.png",
                "id": "a077d4d5-9c2e-45a6-a8c6-2eb42a56de28",
                "name": "Expire",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Machine.Action.InstallTools",
                "description": "Install tools on a machine.",
                "extensionId": null,
                "formScale": null,
                "hasForm": false,
                "iconId": "machineInstallTools.png",
                "id": "7ed95871-d73b-40ba-8c81-7407ef5e4fa1",
                "name": "Install Tools",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Machine.Action.Reset",
                "description": "Reset a machine.",
                "extensionId": null,
                "formScale": null,
                "hasForm": false,
                "iconId": "machineReset.png",
                "id": "b236c593-e0ee-4676-95ed-52ae4b2c2519",
                "name": "Power Cycle",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Machine.Action.PowerOff",
                "description": "Power off a machine.",
                "extensionId": null,
                "formScale": null,
                "hasForm": false,
                "iconId": "machinePowerOff.png",
                "id": "07831a32-0bc7-4f7d-8372-164ea69d61af",
                "name": "Power Off",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Machine.Action.Reboot",
                "description": "Reboot a machine.",
                "extensionId": null,
                "formScale": null,
                "hasForm": false,
                "iconId": "machineReboot.png",
                "id": "bab814c5-6b3c-43cf-8ce2-cd04372d526e",
                "name": "Reboot",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Machine.Action.Reconfigure",
                "description": "Reconfigure a machine.",
                "extensionId": null,
                "formScale": "BIG",
                "hasForm": true,
                "iconId": "machineReconfigure.png",
                "id": "649522a2-f9e3-4286-ad51-f35eb73b6548",
                "name": "Reconfigure",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Machine.Action.Reprovision",
                "description": "Reprovision a machine.",
                "extensionId": null,
                "formScale": "BIG",
                "hasForm": true,
                "iconId": "machineReprovision.png",
                "id": "a19b6255-a6e6-4821-a5a0-bfcb5179bd0e",
                "name": "Reprovision",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Virtual.Action.RevertSnapshot",
                "description": "Revert to a snapshot of this machine.",
                "extensionId": null,
                "formScale": "SMALL",
                "hasForm": true,
                "iconId": "virtualRevertSnapshot.png",
                "id": "64936231-171f-4e32-99cb-57e872d892ee",
                "name": "Revert To Snapshot",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "ycommerce!::!6cbbd9d5-eebe-4b59-b794-08285f9ff5f9",
                "description": "Run puppet on one only Virtual Machine other than a HDBC.",
                "extensionId": null,
                "formScale": "BIG",
                "hasForm": true,
                "iconId": "298480d7-b094-4580-a703-e4212429656d",
                "id": "298480d7-b094-4580-a703-e4212429656d",
                "name": "Run Puppet on VM",
                "providerTypeId": "com.vmware.csp.core.designer.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Machine.Action.Shutdown",
                "description": "Shutdown a machine.",
                "extensionId": null,
                "formScale": null,
                "hasForm": false,
                "iconId": "machineShutdown.png",
                "id": "993ae711-88b9-4baa-9678-4cde5694a703",
                "name": "Shutdown",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            },
            {
                "@type": "ConsumerResourceOperation",
                "bindingId": "Infrastructure.Machine.Action.Suspend",
                "description": "Suspend a machine.",
                "extensionId": null,
                "formScale": null,
                "hasForm": false,
                "iconId": "machineSuspend.png",
                "id": "2073db21-f708-44ab-b5b1-f80cf99e7b09",
                "name": "Suspend",
                "providerTypeId": "com.vmware.csp.iaas.blueprint.service",
                "type": "ACTION"
            }
        ],
        "links": []
    },

Tags: nameidtypedescriptionmachinenullinfrastructurecsp
1条回答
网友
1楼 · 发布于 2024-09-30 01:34:49

我使用任务的“block”来获取“id”的值,所以在block下创建一个任务,这样就可以在块内的所有任务中使用id的值。你知道吗

tasks:
 - name: Getting actions list
   block: 
    - name: get actions list
      uri: xxxxxxx
      ......
      register: actions

    - name: Saving the id value in the variable id
      set_fact:
        id={{ item.id }}
      when: "'Power off a machine' in item.description"
      with_items:
       "{{ actions.json.content }}"
    - name: Printing the Value of id
      debug: var=id

相关问题 更多 >

    热门问题