random list choice within a retries loop

Hi,

I am trying to create new environments, each with a distinct name. Multiple people can run the playbook to create an environment, so I have to avoid conflicts. I have a list of ~200 moon names, and I am trying to grab an AWS DNS record (which I’m assuming is effectively atomic-ish). If I can get a DNS record, then I assume that the name is mine and continue to use it to name the rest of my environment resources.

I tried both random_choice and the random filter, but it seems that when using ‘retries’ on a task the exact same task object is retried. I had this might re-exec the task and pick out a new random choice. But I guess the intention behind ‘retries’ is for when a resource (say an EC2 instance) isn’t available yet?

What I am expecting is that I can get a new random choice on each retry. What actually happens is that the same initial random choice is retried, which makes this task either work first time or fail after five retries.

  • name: Reserve DNS cname to avoid race conditions, fail early!
    route53:
    state: present
    zone: “{{ mydomain }}”
    record: “{{ item }}.{{ mydomain }}”
    type: CNAME
    ttl: 7200
    value: 1.1.1.1
    wait: no
    overwrite: no
    register: result
    with_random_choice: “{{ moons }}”
    retries: 5
    until: result.changed == true

(gist: https://gist.github.com/thisdougb/5e88e7a62b7a9f99620cb0a323ccbda5)

Am I correct in thinking that the task is not going to be re-evaluated in the retry mechanism? I don’t think this is a bug, but maybe the documentation could be clearer about the implementation. I could raise an issue/doc-fix for that?

Any alternative ways to achieve this ?

cheers,
Doug

Looping with an inner loop over the whole input list, with some hack, does appear to work. But it’s pretty inefficient:

outer playbook.yml

  • hosts: localhost
    vars:
    moons:
  • moon
  • styx

tasks:

  • include: inner_tasks.yml
    loop: “{{ moons }}”

inner_tasks.yml

  • name: Reserve DNS cname to avoid race conditions, fail early!
    route53:
    state: present
    zone: “aws-far-oeuf.com
    record: “{{ item }}.aws-far-oeuf.com”
    type: CNAME
    ttl: 7200
    value: 1.1.1.1
    wait: no
    overwrite: no
    register: result
    when:

  • name is not defined

  • set_fact:
    name: “{{ item }}”
    when: result.changed == true

ahh, here’s a better hack :slight_smile:

retry 2 times with random list entries: