# Check attempt count in runtime

**URL:** <https://forum.ansible.com/t/check-attempt-count-in-runtime/30967>\
**Category:** Ansible Project\
**Created:** [August 6, 2019, 7:16pm UTC](https://forum.ansible.com/t/check-attempt-count-in-runtime/30967 "2019-08-06T19:16:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zolvaring](https://avatars.discourse-cdn.com/v4/letter/z/ce73a5/32.png) [@Zolvaring](https://forum.ansible.com/u/Zolvaring)\
**Post date:** [August 6, 2019, 7:16pm UTC](https://forum.ansible.com/t/check-attempt-count-in-runtime/30967/1 "2019-08-06T19:16:45Z")

</div>

I’m trying to work with an API in an idempotent and DRY fashion and cannot find a way to do this for the life of me. What I want to do is hit an endpoint with POST, detect if the response indicates the resource already exists and, if so, hit it with a PUT on retry in order to make sure it’s set properly.

What I have is this \<\> just for removing needless info:

- name: Configure my thing  
uri:  
url: https://{{ ansible\_hostname }}:\<api\_endpoint\>  
method: \>-  
{% if results is defined and results.attempts is defined and results.attempts \>= 1 -%}  
PUT  
{%- else -%}  
POST  
{% endif %}  
headers:  
  
body:   
loop:   
register: results  
retries: 1  
until: item is changed  
changed\_when: item.status == 201  
failed\_when:
- item.status != 200
- item.status != 201
- item.status != 409

The condition is never triggered when I run the task and both retries end up with POST. I tried the simpler method jinja2 of "{% if results is not defined %}POST{% else %}PUT{% endif %} but this never triggers as well, and it seems like even on attempt 2 I cannot access `results` to actually trigger any useful modifications to task parameters.

I’d really appreciate any help if anyone knows what can sort me out here. I realize I can break this up into two or more tasks and have versions that do that but I am looking for the best solution here and to me that means DRY and I’m trying to avoid having multiple tasks or blocks for each action.

---

<div class="post-metadata">

**Author:** ![Zolvaring](https://avatars.discourse-cdn.com/v4/letter/z/ce73a5/32.png) [@Zolvaring](https://forum.ansible.com/u/Zolvaring)\
**Post date:** [August 6, 2019, 7:32pm UTC](https://forum.ansible.com/t/check-attempt-count-in-runtime/30967/2 "2019-08-06T19:32:51Z")

</div>

So I thought of a workaround using nested loops shortly after posting this. I’m going to leave this open because I think that being aware of retries in runtime should be possible, but I wanted to post the solution for anyone else stuck:
