The route53_info module provides a facility for getting a list of Route 53 hosted zones. However, it will only get 100 at a time. (Setting max_items to anything greater than 100 still only provides 100 items.) If the results are truncated, IsTruncated is set to true in the results, and a NextMarker ID is provided that can then be used to make a request for the next batch of zones.

However, I don't see a way to loop in order to ensure that I get all of the zones. The only loop construct that seems to be able to look at the results of the task is until, but it seems to only register the data that results from the last run, and it seems that it doesn't re-expand variables in its arguments, so there's no way to provide an updated next_marker for each iteration.

The only way I see to "ensure" getting all of the zones is to know a maximum number of zones beforehand and just have an appropriate number of instances of the task listed out manually, registering the output of each one individually. Is there another way?


Based on the accepted answer to a very similar question on StackOverflow, I was able to come up with this, which adds collapsing the data gathering into a single looped task, and some additional checking to make sure that all zones were gathered:

- name: List all hosted zones (prime loop)
      NextMarker: ""
      IsTruncated: true
- name: List all hosted zones
    query: hosted_zone
    next_marker: "{{ zone_answer.NextMarker }}"
  when: zone_answer.IsTruncated
  loop: "{{ range(10)|list }}"
  register: zone_answer
- name: List all hosted zones (check for completion)
    msg: "Did not get all zones; increase loop range in task above"
  when: (zone_answer.results | rejectattr('skipped', 'defined') | list | last).IsTruncated
- name: List all hosted zones (compile data)
    hosted_zones: "{{ (zone_answer.results | selectattr('HostedZones', 'defined') | map(attribute='HostedZones') | list | flatten(levels=1)) }}"

(The set_fact can be moved into the vars clause of the playbook, if you find that tidier.)

I'd rather there be a loop that could actually end on its own, rather than have to have a user edit source code to fix it if the list grows too big.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.