# random list choice within a retries loop

**URL:** <https://forum.ansible.com/t/random-list-choice-within-a-retries-loop/29048>\
**Category:** Ansible Developer\
**Tags:** aws\
**Created:** [November 16, 2018, 1:26pm UTC](https://forum.ansible.com/t/random-list-choice-within-a-retries-loop/29048 "2018-11-16T13:26:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doug\_Bridgens](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@Doug\_Bridgens](https://forum.ansible.com/u/Doug_Bridgens)\
**Post date:** [November 16, 2018, 1:26pm UTC](https://forum.ansible.com/t/random-list-choice-within-a-retries-loop/29048/1 "2018-11-16T13:26:14Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Doug\_Bridgens](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@Doug\_Bridgens](https://forum.ansible.com/u/Doug_Bridgens)\
**Post date:** [November 16, 2018, 2:12pm UTC](https://forum.ansible.com/t/random-list-choice-within-a-retries-loop/29048/2 "2018-11-16T14:12:35Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![Doug\_Bridgens](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@Doug\_Bridgens](https://forum.ansible.com/u/Doug_Bridgens)\
**Post date:** [November 16, 2018, 2:44pm UTC](https://forum.ansible.com/t/random-list-choice-within-a-retries-loop/29048/3 "2018-11-16T14:44:39Z")

</div>

ahh, here’s a better hack 🙂

retry 2 times with random list entries:
