简体   繁体   English

如何解决 Ansible 的 url 分页循环问题?

[英]How can I solve the url pagination loop problem with Ansible?

I need to send a URL query and return the JSON result, then read IP, username, password and use it as host.我需要发送一个 URL 查询并返回 JSON 结果,然后读取 IP、用户名、密码并将其用作主机。

And the first step of my problem is that there are multiple pages when I send a URL query and I will save the server information on all these pages as a host, then save the information from these pages as a host, access these servers, and update them.我的问题的第一步是当我发送 URL 查询时有多个页面,我会将所有这些页面上的服务器信息保存为主机,然后将这些页面中的信息保存为主机,访问这些服务器,然后更新它们。

URL - http://apilink/virtual-machines

page results JSON页面结果 JSON

"meta": {
        "pagination": {
            "count": 16, 
            "current_page": 1, 
            "links": {
                "next": "http://apilink/virtual-machines?page=2"
            }, 
            "per_page": 16, 
            "total": 169, 
            "total_pages": 11
        }
    }

I will continue to query with the next URL and I have to write the results on all returned pages as hosts.我将继续查询下一个 URL并且我必须将结果写在所有返回的页面上作为主机。

"json": {
    "data": [
         {
            "actual_mhz": 0, 
            "connectionParameters": {
                "data": [
                    {
                        "description": "Secure Shell", 
                        "ip_addr": "192.168.1.1", 
                        "port": 22, 
                        "protocol": "SSH"
                    }
                ]
            },  
            "hostname": "hostnameserver1", 
            "name": "server1", 
            "os": null, 
            "password": "xXxXxXxXX", 
            "status": "running", 
            "username": "root", 
         [
    },

From the result returned like this, I will use IP, username, password as host, access these hosts with the next task, and run the system update command.从这样返回的结果中,我将使用 IP,用户名,密码作为主机,通过下一个任务访问这些主机,并运行系统更新命令。

In this case, I would do a first API call, just to grab the meta.pagination.total_pages , then based on this value, I would use a couple loop and range , which is the replacement of the with_sequence .在这种情况下,我将执行第一个 API 调用,只是为了获取meta.pagination.total_pages ,然后基于此值,我将使用一对looprange这是with_sequence的替换

Then what you have to know is that when you register the result of a command, you can access the result of the previous items from the variable already.那么你要知道的是,当你注册一个命令的结果时,你已经可以从变量中访问前面各项的结果了。
There is only something to understand, this is the fact that Ansible is creating results in a very peculiar way:有一点需要理解,这是 Ansible 以一种非常奇特的方式创建结果的事实:

  1. The items are registered as if you where using no loop at all, and you can reference the previous element via your registered variable.这些项目的注册就像您根本不使用循环一样,您可以通过注册的变量引用前一个元素。
  2. When you exist the loop, then the results key is created in the dictionary and then populated from all the results.当您存在循环时,将在字典中创建results键,然后从所有结果中填充。

Something like:就像是:

- hosts: localhost
  gather_facts: no
      
  tasks:
    - uri:
        url: https://example.org/virtual-machines
      register: number_of_pages

    - uri: 
        url: >-
          {{ 
             api_call.json.meta.pagination.links.next 
             if api_call is defined else 'https://example.org/virtual-machines'
          }}
      loop: "{{ range(number_of_pages.json.meta.pagination.total_pages) }}"
      register: api_call

    - debug: 
        msg: "{{ api_call }}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM