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:
      <some headers>
    body: <body>
  loop: <series of requests>
  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.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/23cf1a47-7959-4020-9a2a-383e8ff3f0be%40googlegroups.com.

Reply via email to