如何在使用ansible时从dict获得值返回

时间:2018-05-16 06:51:22

标签: python ansible jinja2

我计划最近通过ansible致电vRA api。我可以从API使用uri模式返回json结果。但我怎样才能获得#34;重新启动一台机器。"在将请求发布到API之前,id(" 07831a32-0bc7-4f7d-8372-164ea69d61af")的值。

这里的逻辑首先是循环内容项,然后保存id.value(" 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": []
    },

1 个答案:

答案 0 :(得分:1)

我用"阻止"获得" id"的价值的任务,因此在块下创建一个任务,以便您可以在块内的任务中使用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