使用Ansible在Route 53中列出100多条记录

使用Ansible在Route 53中列出100多条记录,ansible,amazon-route53,ansible-facts,Ansible,Amazon Route53,Ansible Facts,我目前正在一个项目中使用该模块。我在一个托管区域中有250个记录集。我很难列出该区域中的所有记录集。Route 53 API一次最多返回100条记录的页面。为了检索下一页,必须将NextRecordName响应值传递给route53\u facts模块的start\u record\u name:字段(非常简单) 我要特别强调的是,我有责任这样做。人们可能会使用循环来实现这一点,例如在伪代码中: start get 100 records do until response does not c

我目前正在一个项目中使用该模块。我在一个托管区域中有250个记录集。我很难列出该区域中的所有记录集。Route 53 API一次最多返回100条记录的页面。为了检索下一页,必须将NextRecordName响应值传递给route53\u facts模块的start\u record\u name:字段(非常简单)

我要特别强调的是,我有责任这样做。人们可能会使用循环来实现这一点,例如在伪代码中:

start
get 100 records
do until response does not contain NextRecordName:
    get 100 records (start_record_name=NextRecordName)
end
在Ansible中,我编写了以下任务来完成此任务:

- block:
  - name: List record sets in a given hosted zone
    route53_facts:
      query: record_sets
      hosted_zone_id: "/hostedzone/ZZZ1111112222"
      max_items: 100
      start_record_name: "{{ record_sets.NextRecordName | default(omit) }}"
    register: record_sets
    until: record_sets.NextRecordName is not defined
  when: "'{{ hosted_zone['Name'] }}' == 'test.example.com.'"
……然而,这并没有如预期的那样起作用。它不是不断地对响应进行分页,直到不再留下记录,而是重复返回前100条记录(“第一页”)

正如我从Ansible调试输出中所看到的,开始记录\u名称:重复为null

"attempts": 2,
"changed": false,
"invocation": {
    "module_args": {
        "aws_access_key": null,
        "aws_secret_key": null,
        "change_id": null,
        "delegation_set_id": null,
        "dns_name": null,
        "ec2_url": null,
        "health_check_id": null,
        "health_check_method": "list",
        "hosted_zone_id": "/hostedzone/ZZZ1111112222",
        "hosted_zone_method": "list",
        "max_items": "100",
        "next_marker": null,
        "profile": null,
        "query": "record_sets",
        "region": null,
        "resource_id": null,
        "security_token": null,
        "start_record_name": null,
        "type": null,
        "validate_certs": true
    }
},
…我猜,|默认(省略)过滤器总是在执行。换句话说,record\u set.NextRecordName在任务中的这一点上永远不会初始化

我希望有人能帮助我让Ansible归还53号公路某个区域的所有记录。我想我被Ansible的循环行为搞得一团糟。谢谢

用“尽我所能”来警告:

为了回答您的问题,实际上似乎
直到:
注册:
的交互方式与
何时:
注册:
的交互方式不同。我得到的最好的解释是
until:
的行为类似于数据库事务:如果条件为false,它将回滚
寄存器:
赋值,这意味着当再次尝试
until:
任务的主体时,它将使用与第一次相同的参数。使
直到:
块不成为无限循环的唯一方法是
重试:

因此,在您的具体案例中,我认为这将起到作用:

- name: initial record_set
  route53_facts:
  # bootstrap so the upcoming "when:" will evaluate correctly
  register: record_facts
- set_fact:
    # capture the initial answer
    records0: '{{ record_facts.ResourceRecordSets }}'
- name: rest of them
  route53_facts:
    start_record_name: '{{ record_facts.NextRecordName }}'
  register: record_facts
  when: record_facts.NextRecordName | default("")
  with_sequence: count=10
- set_fact:
    all_records: >-
      {{ record0 + (record_facts.results | 
           selectattr("ResourceRecordSets", "defined") |
           map(attribute="ResourceRecordSets") | list) }}
with\u sequence:
是一个hack,因为
循环:
(其中
with\u*
是syntitatic sugar)需要一个要迭代的项目列表,但是如果没有
NextRecordName
返回的响应将导致
当:
失败时,跳过它们,则会使3到10项几乎立即解决

然后,您只需从
route53\u facts:
回复的now
列表中拉出实际的响应数据,并将它们粘贴到初始数据以获得完整的列表



说了所有这些,我现在确信
route53\u事实:
(以及将迭代负担推到剧本中的任何其他AWS模块)行为是一个缺陷。模块调用者已经有一个
max_items:
可供使用,但这是一个实现细节,任何值都不能大于某个随机分页截止值。

您的问题显然非常相关:但根据他们的回答,我不确定它是否会在任何时候修复,谢谢您,马太福音。我可以确认这种方法对我有效。这里需要注意的是,“with_sequence:count=10”最多只能使用(我认为)1000个记录集(最多100个项目*10)。无论如何,这让我摆脱了一个非常令人沮丧的局面——再次感谢!有关此答案的可能更新,请参阅。