Ansible:如何处理扁平化事实

时间:2019-02-05 08:37:26

标签: ansible

我需要帮助来使用Ansible创建剧本。我不明白如何使用循环。

1)我调用API Rest,然后将结果实际上(它是一个列表)

2)我想在每个元素上循环并使用

调用其他API Rest

我的代码:

  tasks:

  - name: API  to obtain share id
    uri:
      url: "https://{{ rub_host }}/api/v1/sla_domain?primary_cluster_id=local"
      method: GET
      user: "{{ rub_login }}"
      password: "{{ rub_password }}"
      force_basic_auth: yes
      return_content: yes
      body_format: json
      status_code: 200
    register: "listslaid_response"

  - name: store the result
    set_fact:
      listslaid: "{{ listslaid_response.json | json_query(\"data[].id\") }}"

  - name: "display each item which are slaid"
    debug:
      msg: "An item: {{ item }}"
    with_items: "{{ listslaid }}"

  - name: API  to list detail of all sla
    uri:
      url: "https://{{ rub_host }}/api/v1/sla_domain/{{ item }}"
      method: GET
      user: "{{ rub_login }}"
      password: "{{ rub_password }}"
      force_basic_auth: yes
      return_content: yes
      body_format: json
      status_code: 200
    register: "listdetailsla_response"
    with_items: "{{ listslaid }}"

  - name: "display each item which are slaid"
    debug:
      msg: "{{ listdetailsla_response }}"

我在Ansible的文档中搜索了如何使用模块循环,但对于我的情况我不理解。

我的输出摘录:

                "msg": "OK (673 bytes)",
                "redirected": false,
                "server": "spray-can/1.3.3",
                "status": 200,
                "strict_transport_security": "max-age=31536000",
                "url": "https:///api/v1/sla_domain/c62c3e54-be2c-460f-a67a-0df59f80e663",
                "x_content_type_options": "nosniff",
                "x_frame_options": "SAMEORIGIN",
                "x_permitted_cross_domain_policies": "none",
                "x_xss_protection": "1; mode=block"
            },
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "cache_control": "private",
                "changed": false,
                "connection": "close",
                "content": "{\"id\":\"c8349148-fe99-45c4-95be-5c7f6481be3e\",\"primaryClusterId\":\"1b948391-2e54-4b5e-b72e-737c815d9de8\",\"name\":\"Bronze_Midi\",\"frequencies\":[{\"timeUnit\":\"Daily\",\"frequency\":1,\"retention\":32}],\"allowedBackupWindows\":[{\"startTimeAttributes\":{\"minutes\":0,\"hour\":12},\"durationInHours\":2}],\"firstFullAllowedBackupWindows\":[],\"maxLocalRetentionLimit\":2764800,\"archivalSpecs\":[],\"replicationSpecs\":[],\"numDbs\":0,\"numFilesets\":0,\"numHypervVms\":0,\"numNutanixVms\":0,\"numManagedVolumes\":0,\"numStorageArrayVolumeGroups\":0,\"numWindowsVolumeGroups\":0,\"numLinuxHosts\":0,\"numShares\":0,\"numWindowsHosts\":0,\"numVms\":1,\"numEc2Instances\":0,\"numVcdVapps\":0,\"isDefault\":false,\"uiColor\":\"#1fccc7\"}",
                "content_length": "672",
                "content_security_policy": "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://csi.gstatic.com; script-src 'self' 'unsafe-eval' 'unsafe-inline' https://maps.googleapis.com; font-src 'self' data:;",
                "content_type": "application/json; charset=UTF-8",
                "cookies": {},
                "date": "Tue, 05 Feb 2019 14:16:08 GMT",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "attributes": null,
                        "backup": null,
                        "body": null,
                        "body_format": "json",
                        "client_cert": null,
                        "client_key": null,
                        "content": null,
                        "creates": null,
                        "delimiter": null,
                        "dest": null,
                        "directory_mode": null,
                        "follow": false,
                        "follow_redirects": "safe",
                        "force": false,
                        "force_basic_auth": true,
                        "group": null,
                        "headers": {
                            "Authorization": "Basic YWRtaW46ODk5REthbUxweTZN",
                            "Content-Type": "application/json"
                        },
                        "http_agent": "ansible-httpget",
                        "method": "GET",
                        "mode": null,
                        "owner": null,
                        "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "regexp": null,
                        "remote_src": null,
                        "removes": null,
                        "return_content": true,
                        "selevel": null,
                        "serole": null,
                        "setype": null,
                        "seuser": null,
                        "src": null,
                        "status_code": [
                            "200"
                        ],
                        "timeout": 30,
                        "unsafe_writes": null,
                        "url": "https:///api/v1/sla_domain/c8349148-fe99-45c4-95be-5c7f6481be3e",
                        "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                        "url_username": "admin",
                        "use_proxy": true,
                        "user": "admin",
                        "validate_certs": true
                    }
                },
                "item": "c8349148-fe99-45c4-95be-5c7f6481be3e",
                "json": {
                    "allowedBackupWindows": [
                        {
                            "durationInHours": 2,
                            "startTimeAttributes": {
                                "hour": 12,
                                "minutes": 0
                            }
                        }
                    ],
                    "archivalSpecs": [],
                    "firstFullAllowedBackupWindows": [],
                    "frequencies": [
                        {
                            "frequency": 1,
                            "retention": 32,
                            "timeUnit": "Daily"
                        }
                    ],
                    "id": "c8349148-fe99-45c4-95be-5c7f6481be3e",
                    "isDefault": false,
                    "maxLocalRetentionLimit": 2764800,
                    "name": "Bronze_Midi",
                    "numDbs": 0,
                    "numEc2Instances": 0,
                    "numFilesets": 0,
                    "numHypervVms": 0,
                    "numLinuxHosts": 0,
                    "numManagedVolumes": 0,
                    "numNutanixVms": 0,
                    "numShares": 0,
                    "numStorageArrayVolumeGroups": 0,
                    "numVcdVapps": 0,
                    "numVms": 1,
                    "numWindowsHosts": 0,
                    "numWindowsVolumeGroups": 0,
                    "primaryClusterId": "1b948391-2e54-4b5e-b72e-737c815d9de8",
                    "replicationSpecs": [],
                    "uiColor": "#1fccc7"
                },
                "msg": "OK (672 bytes)",
                "redirected": false,
                "server": "spray-can/1.3.3",
                "status": 200,
                "strict_transport_security": "max-age=31536000",
                "url": "https:///api/v1/sla_domain/c8349148-fe99-45c4-95be-5c7f6481be3e",
                "x_content_type_options": "nosniff",
                "x_frame_options": "SAMEORIGIN",
                "x_permitted_cross_domain_policies": "none",
                "x_xss_protection": "1; mode=block"
            }
        ]
    }
}

使用jmespath,我找到了此过滤器:msg.results []。json,但我不知道如何在输出中使用它。

我尝试了json_query,但失败了。我只用管子尝试,但再次失败。

我如何过滤结果?

0 个答案:

没有答案